Org.BouncyCastle.Math.BigInteger.SetBit C# (CSharp) Метод

SetBit() публичный Метод

public SetBit ( int n ) : BigInteger
n int
Результат BigInteger
		public BigInteger SetBit(
			int n)
		{
			if (n < 0)
				throw new ArithmeticException("Bit address less than zero");

			if (TestBit(n))
				return this;

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

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

Usage Example

Пример #1
0
		public void TestSetBit()
		{
			Assert.AreEqual(one, zero.SetBit(0));
			Assert.AreEqual(one, one.SetBit(0));
			Assert.AreEqual(three, two.SetBit(0));

			Assert.AreEqual(two, zero.SetBit(1));
			Assert.AreEqual(three, one.SetBit(1));
			Assert.AreEqual(two, two.SetBit(1));

			// TODO Tests for setting bits in negative numbers

			// TODO Tests for setting extended bits

			for (int i = 0; i < 10; ++i)
			{
				BigInteger n = new BigInteger(128, random);

				for (int j = 0; j < 10; ++j)
				{
					int pos = random.Next(128);
					BigInteger m = n.SetBit(pos);
					bool test = m.ShiftRight(pos).Remainder(two).Equals(one);

					Assert.IsTrue(test);
				}
			}

			for (int i = 0; i < 100; ++i)
			{
				BigInteger pow2 = one.ShiftLeft(i);
				BigInteger minusPow2 = pow2.Negate();

				Assert.AreEqual(pow2, pow2.SetBit(i));
				Assert.AreEqual(minusPow2, minusPow2.SetBit(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.Or(one.ShiftLeft(j)), bigI.SetBit(j), data);
					Assert.AreEqual(negI.Or(one.ShiftLeft(j)), negI.SetBit(j), data);
				}
			}
		}
All Usage Examples Of Org.BouncyCastle.Math.BigInteger::SetBit