public void TestFlipBit() { for (int i = 0; i < 10; ++i) { BigInteger a = new BigInteger(128, 0, random); BigInteger b = a; for (int x = 0; x < 100; ++x) { // Note: Intentionally greater than initial size int pos = random.Next(256); a = a.FlipBit(pos); b = b.TestBit(pos) ? b.ClearBit(pos) : b.SetBit(pos); } Assert.AreEqual(a, b); } for (int i = 0; i < 100; ++i) { BigInteger pow2 = one.ShiftLeft(i); BigInteger minusPow2 = pow2.Negate(); Assert.AreEqual(zero, pow2.FlipBit(i)); Assert.AreEqual(minusPow2.ShiftLeft(1), minusPow2.FlipBit(i)); BigInteger bigI = BigInteger.ValueOf(i); BigInteger negI = bigI.Negate(); for (int j = 0; j < 10; ++j) { string data = "i=" + i + ", j=" + j; Assert.AreEqual(bigI.Xor(one.ShiftLeft(j)), bigI.FlipBit(j), data); Assert.AreEqual(negI.Xor(one.ShiftLeft(j)), negI.FlipBit(j), data); } } }