Org.BouncyCastle.Math.BigInteger.FlipBit C# (CSharp) Method

FlipBit() public method

public FlipBit ( int n ) : BigInteger
n int
return BigInteger
		public BigInteger FlipBit(
			int n)
		{
			if (n < 0)
				throw new ArithmeticException("Bit address less than zero");

			// TODO Handle negative values and zero
			if (sign > 0 && n < (BitLength - 1))
				return FlipExistingBit(n);

			return Xor(One.ShiftLeft(n));
		}

Usage Example

コード例 #1
0
ファイル: BigIntegerTest.cs プロジェクト: randombit/hacrypto
		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);
				}
			}
		}