Jurassic.BigInteger.LeftShift C# (CSharp) Method

LeftShift() public static method

Shifts a BigInteger value a specified number of bits to the left.
public static LeftShift ( 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 left. /// Can be negative to shift to the right.
return BigInteger
        public static BigInteger LeftShift(BigInteger value, int shift)
        {
            // Shifting by zero bits does nothing.
            if (shift == 0)
                return value;

            // Shifting left by a negative number of bits is the same as shifting right.
            if (shift < 0)
                return RightShift(value, -shift);

            int wordShift = shift / 32;
            int bitShift = shift - (wordShift * 32);

            uint[] outputBits = new uint[value.wordCount + wordShift + 1];
            int outputWordCount = outputBits.Length - 1;

            uint carry = 0;
            for (int i = 0; i < value.wordCount; i++)
            {
                uint word = value.bits[i];
                outputBits[i + wordShift] = (word << bitShift) | carry;
                carry = bitShift == 0 ? 0 : word >> (32 - bitShift);
            }
            if (carry != 0)
            {
                outputBits[outputWordCount] = carry;
                outputWordCount++;
            }

            return new BigInteger(outputBits, outputWordCount, value.sign);
        }

Usage Example

示例#1
0
        /// <summary>
        /// Scales the given double-precision number by multiplying and then shifting it.
        /// </summary>
        /// <param name="value"> The value to scale. </param>
        /// <param name="multiplier"> The multiplier. </param>
        /// <param name="shift"> The power of two scale factor. </param>
        /// <returns> A BigInteger containing the result of multiplying <paramref name="value"/> by
        /// <paramref name="multiplier"/> and then shifting left by <paramref name="shift"/> bits. </returns>
        private static BigInteger ScaleToInteger(double value, BigInteger multiplier, int shift)
        {
            long bits = BitConverter.DoubleToInt64Bits(value);

            // Extract the base-2 exponent.
            var base2Exponent = (int)((bits & 0x7FF0000000000000) >> 52) - 1023;

            // Extract the mantissa.
            long mantissa = bits & 0xFFFFFFFFFFFFF;

            if (base2Exponent > -1023)
            {
                mantissa      |= 0x10000000000000;
                base2Exponent -= 52;
            }
            else
            {
                // Denormals.
                base2Exponent -= 51;
            }

            // Extract the sign bit.
            if (bits < 0)
            {
                mantissa = -mantissa;
            }

            var result = new BigInteger(mantissa);

            result = BigInteger.Multiply(result, multiplier);
            shift += base2Exponent;
            result = BigInteger.LeftShift(result, shift);
            return(result);
        }
All Usage Examples Of Jurassic.BigInteger::LeftShift