public static BigInteger RightShift(BigInteger value, int shift)
{
// Shifting by zero bits does nothing.
if (shift == 0)
return value;
// Shifting right by a negative number of bits is the same as shifting left.
if (shift < 0)
return LeftShift(value, -shift);
int wordShift = shift / 32;
int bitShift = shift - (wordShift * 32);
if (wordShift >= value.wordCount)
return BigInteger.Zero;
uint[] outputBits = new uint[value.wordCount - wordShift];
int outputWordCount = outputBits.Length - 1;
uint carry = 0;
for (int i = value.wordCount - 1; i >= wordShift; i--)
{
uint word = value.bits[i];
outputBits[i - wordShift] = (word >> bitShift) | (carry << (32 - bitShift));
carry = word & (((uint)1 << bitShift) - 1);
}
if (outputBits[outputBits.Length - 1] != 0)
outputWordCount++;
return new BigInteger(outputBits, outputWordCount, value.sign);
}