Jurassic.BigInteger.RightShift C# (CSharp) Method

RightShift() public static method

Shifts a BigInteger value a specified number of bits to the right.
Note: unlike System.Numerics.BigInteger, negative numbers are treated identically to positive numbers.
public static RightShift ( BigInteger value, int shift ) : BigInteger
value BigInteger The value whose bits are to be shifted.
shift int The number of bits to shift to the right.
return BigInteger
        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);
        }

Usage Example

示例#1
0
        /// <summary>
        /// Returns a new instance BigInteger structure from a 64-bit double precision floating
        /// point value.
        /// </summary>
        /// <param name="value"> A 64-bit double precision floating point value. </param>
        /// <returns> The corresponding BigInteger value. </returns>
        public double ToDouble()
        {
            // Special case: zero.
            if (this.wordCount == 1 && this.bits[0] == 0)
            {
                return(0.0);
            }

            // Get the number of bits in the BigInteger.
            var bitCount = this.BitCount;

            // The top 53 bits can be packed into the double (the top-most bit is implied).
            var   temp       = BigInteger.RightShift(this, bitCount - 53);
            ulong doubleBits = (((ulong)temp.bits[1] << 32) | temp.bits[0]) & 0xFFFFFFFFFFFFF;

            // Base-2 exponent is however much we shifted, plus 52 (because the decimal point is
            // effectively at the 52nd bit), plus 1023 (the bias).
            doubleBits |= (ulong)(bitCount - 53 + 52 + 1023) << 52;

            // Handle the sign bit.
            if (this.sign == -1)
            {
                doubleBits |= (ulong)1 << 63;
            }

            // Convert the bit representation to a double.
            return(XBitConverter.Int64BitsToDouble((long)doubleBits));
        }
All Usage Examples Of Jurassic.BigInteger::RightShift