当前位置: 首页>>代码示例>>C#>>正文


C# BigInteger.CutOffLeadingZeroes方法代码示例

本文整理汇总了C#中BigInteger.CutOffLeadingZeroes方法的典型用法代码示例。如果您正苦于以下问题:C# BigInteger.CutOffLeadingZeroes方法的具体用法?C# BigInteger.CutOffLeadingZeroes怎么用?C# BigInteger.CutOffLeadingZeroes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在BigInteger的用法示例。


在下文中一共展示了BigInteger.CutOffLeadingZeroes方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: AndNotPositiveNegative

        private static BigInteger AndNotPositiveNegative(BigInteger Positive, BigInteger Negative)
        {
            // PRE: positive > 0 && negative < 0
            int iNeg = Negative.FirstNonzeroDigit;
            int iPos = Positive.FirstNonzeroDigit;

            if (iNeg >= Positive._numberLength)
                return Positive;

            int resLength = System.Math.Min(Positive._numberLength, Negative._numberLength);
            int[] resDigits = new int[resLength];

            // Always start from first non zero of positive
            int i = iPos;

            for (; i < iNeg; i++)
                resDigits[i] = Positive._digits[i];

            if (i == iNeg)
            {
                resDigits[i] = Positive._digits[i] & (Negative._digits[i] - 1);
                i++;
            }
            for (; i < resLength; i++)
                resDigits[i] = Positive._digits[i] & Negative._digits[i];

            BigInteger result = new BigInteger(1, resLength, resDigits);
            result.CutOffLeadingZeroes();

            return result;
        }
开发者ID:DeadlyEmbrace,项目名称:NTRU-NET,代码行数:31,代码来源:Logical.cs

示例2: ShiftLeftOneBit

        /// <summary>
        /// Shifts the source digits left one bit, creating a value whose magnitude is doubled.
        /// </summary>
        /// 
        /// <param name="Value">The source BigIntger</param>
        internal static BigInteger ShiftLeftOneBit(BigInteger Value)
        {
            int srcLen = Value._numberLength;
            int resLen = srcLen + 1;
            int[] resDigits = new int[resLen];
            ShiftLeftOneBit(resDigits, Value._digits, srcLen);
            BigInteger result = new BigInteger(Value._sign, resLen, resDigits);
            result.CutOffLeadingZeroes();

            return result;
        }
开发者ID:DeadlyEmbrace,项目名称:NTRU-NET,代码行数:16,代码来源:BitLevel.cs

示例3: MultiplyByPositiveInt

        /// <summary>
        /// Multiplies a number by a positive integer.
        /// </summary>
        /// 
        /// <param name="X">An arbitrary BigInteger</param>
        /// <param name="Factor">A positive int number</param>
        /// 
        /// <returns>X * Factor</returns>
        internal static BigInteger MultiplyByPositiveInt(BigInteger X, int Factor)
        {
            int resSign = X._sign;
            if (resSign == 0)
                return BigInteger.Zero;

            int aNumberLength = X._numberLength;
            int[] aDigits = X._digits;

            if (aNumberLength == 1)
            {
                long res = UnsignedMultAddAdd(aDigits[0], Factor, 0, 0);
                int resLo = (int)res;
                int resHi = (int)IntUtils.URShift(res, 32);

                return ((resHi == 0) ?
                    new BigInteger(resSign, resLo) :
                    new BigInteger(resSign, 2, new int[] { resLo, resHi }));
            }
            // Common case
            int resLength = aNumberLength + 1;
            int[] resDigits = new int[resLength];

            resDigits[aNumberLength] = MultiplyByInt(resDigits, aDigits, aNumberLength, Factor);
            BigInteger result = new BigInteger(resSign, resLength, resDigits);
            result.CutOffLeadingZeroes();

            return result;
        }
开发者ID:jesusgarza,项目名称:McEliece-Sharp,代码行数:37,代码来源:Multiplication.cs

示例4: FlipBit

        /// <summary>
        /// Returns a new BigInteger which has the same binary representation 
        /// as this but with the bit at position N flipped. 
        /// <para>The result is equivalent to this ^ 2^N.</para>
        /// </summary>
        /// 
        /// <param name="Value">The source BigIntger</param>
        /// <param name="N">Position where the bit in this has to be flipped</param>
        /// 
        /// <returns>Returns <c>this ^ 2^N</c></returns>
        /// 
        /// <exception cref="ArithmeticException">Thrown if a negative bit address is used</exception>
        internal static BigInteger FlipBit(BigInteger Value, int N)
        {
            int resSign = (Value._sign == 0) ? 1 : Value._sign;
            int intCount = N >> 5;
            int bitN = N & 31;
            int resLength = System.Math.Max(intCount + 1, Value._numberLength) + 1;
            int[] resDigits = new int[resLength];
            int i;

            int bitNumber = 1 << bitN;
            Array.Copy(Value._digits, 0, resDigits, 0, Value._numberLength);

            if (Value._sign < 0)
            {
                if (intCount >= Value._numberLength)
                {
                    resDigits[intCount] = bitNumber;
                }
                else
                {
                    //val.sign<0 y intCount < val.numberLength
                    int firstNonZeroDigit = Value.FirstNonzeroDigit;
                    if (intCount > firstNonZeroDigit)
                    {
                        resDigits[intCount] ^= bitNumber;
                    }
                    else if (intCount < firstNonZeroDigit)
                    {
                        resDigits[intCount] = -bitNumber;
                        for (i = intCount + 1; i < firstNonZeroDigit; i++)
                            resDigits[i] = -1;

                        resDigits[i] = resDigits[i]--;
                    }
                    else
                    {
                        i = intCount;
                        resDigits[i] = -((-resDigits[intCount]) ^ bitNumber);
                        if (resDigits[i] == 0)
                        {
                            for (i++; resDigits[i] == -1; i++)
                                resDigits[i] = 0;

                            resDigits[i]++;
                        }
                    }
                }
            }
            else
            {   //case where val is positive
                resDigits[intCount] ^= bitNumber;
            }

            BigInteger result = new BigInteger(resSign, resLength, resDigits);
            result.CutOffLeadingZeroes();

            return result;
        }
开发者ID:DeadlyEmbrace,项目名称:NTRU-NET,代码行数:70,代码来源:BitLevel.cs

示例5: InplaceShiftRight

        /// <summary>
        /// Performs Value >>= count where Value is a positive number.
        /// </summary>
        /// 
        /// <param name="Value">The source BigIntger</param>
        /// <param name="N">Shift distance</param>
        internal static void InplaceShiftRight(BigInteger Value, int N)
        {
            int sign = Value.Signum();

            if (N == 0 || Value.Signum() == 0)
                return;

            int intCount = N >> 5; // count of integers
            Value._numberLength -= intCount;
            if (!ShiftRight(Value._digits, Value._numberLength, Value._digits, intCount, N & 31) && sign < 0)
            {
                // remainder not zero: add one to the result
                int i;
                for (i = 0; (i < Value._numberLength) && (Value._digits[i] == -1); i++)
                    Value._digits[i] = 0;

                if (i == Value._numberLength)
                    Value._numberLength++;

                Value._digits[i]++;
            }
            Value.CutOffLeadingZeroes();
            Value.UnCache();
        }
开发者ID:DeadlyEmbrace,项目名称:NTRU-NET,代码行数:30,代码来源:BitLevel.cs

示例6: InplaceAdd

 /// <summary>
 /// Performs Op1 += Op2.
 /// <para>Op1 must have enough place to store the result (i.e. Op1.BitLength() >= Op2.BitLength()). 
 /// Both should be positive (i.e. Op1 >= Op2).</para>
 /// </summary>
 /// 
 /// <param name="A">The input minuend, and the output result</param>
 /// <param name="B">The addend</param>
 internal static void InplaceAdd(BigInteger A, BigInteger B)
 {
     // op1 >= op2 > 0
     Add(A._digits, A._digits, A._numberLength, B._digits, B._numberLength);
     A._numberLength = System.Math.Min(System.Math.Max(A._numberLength, B._numberLength) + 1, A._digits.Length);
     A.CutOffLeadingZeroes();
     A.UnCache();
 }
开发者ID:DeadlyEmbrace,项目名称:NTRU-NET,代码行数:16,代码来源:Elementary.cs

示例7: Subtract

        /// <summary>
        /// See BigInteger#subtract(BigInteger)
        /// </summary>
        internal static BigInteger Subtract(BigInteger A, BigInteger B)
        {
            int resSign;
            int[] resDigits;
            int op1Sign = A._sign;
            int op2Sign = B._sign;

            if (op2Sign == 0)
                return A;
            if (op1Sign == 0)
                return B.Negate();

            int op1Len = A._numberLength;
            int op2Len = B._numberLength;
            if (op1Len + op2Len == 2)
            {
                long a = (A._digits[0] & 0xFFFFFFFFL);
                long b = (B._digits[0] & 0xFFFFFFFFL);

                if (op1Sign < 0)
                    a = -a;
                if (op2Sign < 0)
                    b = -b;

                return BigInteger.ValueOf(a - b);
            }
            int cmp = ((op1Len != op2Len) ?
                ((op1Len > op2Len) ? 1 : -1) :
                Elementary.CompareArrays(A._digits, B._digits, op1Len));

            if (cmp == BigInteger.LESS)
            {
                resSign = -op2Sign;
                resDigits = (op1Sign == op2Sign) ?
                    Subtract(B._digits, op2Len, A._digits, op1Len) :
                    Add(B._digits, op2Len, A._digits, op1Len);
            }
            else
            {
                resSign = op1Sign;
                if (op1Sign == op2Sign)
                {
                    if (cmp == BigInteger.EQUALS)
                        return BigInteger.Zero;

                    resDigits = Subtract(A._digits, op1Len, B._digits, op2Len);
                }
                else
                {
                    resDigits = Add(A._digits, op1Len, B._digits, op2Len);
                }
            }
            BigInteger res = new BigInteger(resSign, resDigits.Length, resDigits);
            res.CutOffLeadingZeroes();

            return res;
        }
开发者ID:DeadlyEmbrace,项目名称:NTRU-NET,代码行数:60,代码来源:Elementary.cs

示例8: DivideAndRemainderByInteger

 /// <summary>
 /// Computes the quotient and the remainder after a division by an int number
 /// </summary>
 /// 
 /// <param name="Value">The BigInteger dividend</param>
 /// <param name="Divisor">The divisor</param>
 /// <param name="DivisorSign">The divisors sign</param>
 /// 
 /// <returns>Returns an array of the form <c>[quotient, remainder]</c></returns>
 internal static BigInteger[] DivideAndRemainderByInteger(BigInteger Value, int Divisor, int DivisorSign)
 {
     // res[0] is a quotient and res[1] is a remainder:
     int[] valDigits = Value._digits;
     int valLen = Value._numberLength;
     int valSign = Value._sign;
     if (valLen == 1)
     {
         long a = (valDigits[0] & 0xffffffffL);
         long b = (Divisor & 0xffffffffL);
         long quo = a / b;
         long rem = a % b;
         if (valSign != DivisorSign)
         {
             quo = -quo;
         }
         if (valSign < 0)
         {
             rem = -rem;
         }
         return new BigInteger[] { BigInteger.ValueOf(quo),
             BigInteger.ValueOf(rem) };
     }
     int quotientLength = valLen;
     int quotientSign = ((valSign == DivisorSign) ? 1 : -1);
     int[] quotientDigits = new int[quotientLength];
     int[] remainderDigits;
     remainderDigits = new int[] { Division.DivideArrayByInt(
         quotientDigits, valDigits, valLen, Divisor) };
     BigInteger result0 = new BigInteger(quotientSign, quotientLength,
             quotientDigits);
     BigInteger result1 = new BigInteger(valSign, 1, remainderDigits);
     result0.CutOffLeadingZeroes();
     result1.CutOffLeadingZeroes();
     return new BigInteger[] { result0, result1 };
 }
开发者ID:DeadlyEmbrace,项目名称:NTRU-NET,代码行数:45,代码来源:Division.cs

示例9: InplaceModPow2

        /// <summary>
        /// Performs <c>X = X Mod (2<sup>N</sup>)</c>
        /// </summary>
        /// <param name="X">A positive number, it will store the result</param>
        /// <param name="N">A positive exponent of 2</param>
        internal static void InplaceModPow2(BigInteger X, int N)
        {
            // PRE: (x > 0) and (n >= 0)
            int fd = N >> 5;
            int leadingZeros;

            if ((X._numberLength < fd) || (X.BitLength <= N))
            {
                return;
            }
            leadingZeros = 32 - (N & 31);
            X._numberLength = fd + 1;
            X._digits[fd] &= (leadingZeros < 32) ? (IntUtils.URShift(-1, leadingZeros)) : 0;
            X.CutOffLeadingZeroes();
        }
开发者ID:DeadlyEmbrace,项目名称:NTRU-NET,代码行数:20,代码来源:Division.cs

示例10: XorNegative

        private static BigInteger XorNegative(BigInteger Value, BigInteger X)
        {
            // PRE: val and that are negative
            // PRE: val has at least as many trailing zero digits as that
            int resLength = System.Math.Max(Value._numberLength, X._numberLength);
            int[] resDigits = new int[resLength];
            int iVal = Value.FirstNonzeroDigit;
            int iThat = X.FirstNonzeroDigit;
            int i = iThat;
            int limit;


            if (iVal == iThat)
            {
                resDigits[i] = -Value._digits[i] ^ -X._digits[i];
            }
            else
            {
                resDigits[i] = -X._digits[i];
                limit = System.Math.Min(X._numberLength, iVal);
                for (i++; i < limit; i++)
                    resDigits[i] = ~X._digits[i];

                // Remains digits in that?
                if (i == X._numberLength)
                {
                    //Jumping over the remaining zero to the first non one
                    for (; i < iVal; i++)
                        resDigits[i] = -1;

                    resDigits[i] = Value._digits[i] - 1;
                }
                else
                {
                    resDigits[i] = -Value._digits[i] ^ ~X._digits[i];
                }
            }

            limit = System.Math.Min(Value._numberLength, X._numberLength);
            //Perform ^ between that al val until that ends
            for (i++; i < limit; i++)
                resDigits[i] = Value._digits[i] ^ X._digits[i];

            //Perform ^ between val digits and -1 until val ends
            for (; i < Value._numberLength; i++)
                resDigits[i] = Value._digits[i];
            for (; i < X._numberLength; i++)
                resDigits[i] = X._digits[i];

            BigInteger result = new BigInteger(1, resLength, resDigits);
            result.CutOffLeadingZeroes();

            return result;
        }
开发者ID:DeadlyEmbrace,项目名称:NTRU-NET,代码行数:54,代码来源:Logical.cs

示例11: XorPositive

        private static BigInteger XorPositive(BigInteger Longer, BigInteger Shorter)
        {
            // PRE: longer and shorter are positive;
            // PRE: longer has at least as many digits as shorter
            int resLength = Longer._numberLength;
            int[] resDigits = new int[resLength];
            int i = System.Math.Min(Longer.FirstNonzeroDigit, Shorter.FirstNonzeroDigit);

            for (; i < Shorter._numberLength; i++)
                resDigits[i] = Longer._digits[i] ^ Shorter._digits[i];
            for (; i < Longer._numberLength; i++)
                resDigits[i] = Longer._digits[i];

            BigInteger result = new BigInteger(1, resLength, resDigits);
            result.CutOffLeadingZeroes();

            return result;
        }
开发者ID:DeadlyEmbrace,项目名称:NTRU-NET,代码行数:18,代码来源:Logical.cs

示例12: XorDiffSigns


//.........这里部分代码省略.........
            int limit;

            //The first
            if (iNeg < iPos)
            {
                resDigits = new int[resLength];
                i = iNeg;
                //resDigits[i] = -(-negative.digits[i]);
                resDigits[i] = Negative._digits[i];
                limit = System.Math.Min(Negative._numberLength, iPos);
                //Skip the positive digits while they are zeros
                for (i++; i < limit; i++)
                    resDigits[i] = Negative._digits[i];

                //if the negative has no more elements, must fill the
                //result with the remaining digits of the positive
                if (i == Negative._numberLength)
                {
                    for (; i < Positive._numberLength; i++)
                        resDigits[i] = Positive._digits[i];
                }
            }
            else if (iPos < iNeg)
            {
                resDigits = new int[resLength];
                i = iPos;
                //Applying two complement to the first non-zero digit of the result
                resDigits[i] = -Positive._digits[i];
                limit = System.Math.Min(Positive._numberLength, iNeg);
                //Continue applying two complement the result
                for (i++; i < limit; i++)
                    resDigits[i] = ~Positive._digits[i];

                //When the first non-zero digit of the negative is reached, must apply
                //two complement (arithmetic negation) to it, and then operate
                if (i == iNeg)
                {
                    resDigits[i] = ~(Positive._digits[i] ^ -Negative._digits[i]);
                    i++;
                }
                else
                {
                    //if the positive has no more elements must fill the remaining digits with
                    //the negative ones
                    for (; i < iNeg; i++)
                        resDigits[i] = -1;
                    for (; i < Negative._numberLength; i++)
                        resDigits[i] = Negative._digits[i];
                }
            }
            else
            {
                int digit;
                //The first non-zero digit of the positive and negative are the same
                i = iNeg;
                digit = Positive._digits[i] ^ -Negative._digits[i];
                if (digit == 0)
                {
                    limit = System.Math.Min(Positive._numberLength, Negative._numberLength);
                    for (i++; i < limit && (digit = Positive._digits[i] ^ ~Negative._digits[i]) == 0; i++)
                        ;
                    if (digit == 0)
                    {
                        // shorter has only the remaining virtual sign bits
                        for (; i < Positive._numberLength && (digit = ~Positive._digits[i]) == 0; i++)
                        {
                            ;
                        }
                        for (; i < Negative._numberLength && (digit = ~Negative._digits[i]) == 0; i++)
                        {
                            ;
                        }
                        if (digit == 0)
                        {
                            resLength = resLength + 1;
                            resDigits = new int[resLength];
                            resDigits[resLength - 1] = 1;

                            return new BigInteger(-1, resLength, resDigits);
                        }
                    }
                }
                resDigits = new int[resLength];
                resDigits[i] = -digit;
                i++;
            }

            limit = System.Math.Min(Negative._numberLength, Positive._numberLength);
            for (; i < limit; i++)
                resDigits[i] = ~(~Negative._digits[i] ^ Positive._digits[i]);
            for (; i < Positive._numberLength; i++)
                resDigits[i] = Positive._digits[i];
            for (; i < Negative._numberLength; i++)
                resDigits[i] = Negative._digits[i];

            BigInteger result = new BigInteger(-1, resLength, resDigits);
            result.CutOffLeadingZeroes();

            return result;
        }
开发者ID:DeadlyEmbrace,项目名称:NTRU-NET,代码行数:101,代码来源:Logical.cs

示例13: OrNegative

        private static BigInteger OrNegative(BigInteger Value, BigInteger X)
        {
            // PRE: val and that are negative;
            // PRE: val has at least as many trailing zeros digits as that
            int iThat = X.FirstNonzeroDigit;
            int iVal = Value.FirstNonzeroDigit;
            int i;

            if (iVal >= X._numberLength)
                return X;
            else if (iThat >= Value._numberLength)
                return Value;

            int resLength = System.Math.Min(Value._numberLength, X._numberLength);
            int[] resDigits = new int[resLength];

            // Looking for the first non-zero digit of the result
            if (iThat == iVal)
            {
                resDigits[iVal] = -(-Value._digits[iVal] | -X._digits[iVal]);
                i = iVal;
            }
            else
            {
                for (i = iThat; i < iVal; i++)
                    resDigits[i] = X._digits[i];

                resDigits[i] = X._digits[i] & (Value._digits[i] - 1);
            }

            for (i++; i < resLength; i++)
                resDigits[i] = Value._digits[i] & X._digits[i];

            BigInteger result = new BigInteger(-1, resLength, resDigits);
            result.CutOffLeadingZeroes();

            return result;
        }
开发者ID:DeadlyEmbrace,项目名称:NTRU-NET,代码行数:38,代码来源:Logical.cs

示例14: OrDiffSigns

        private static BigInteger OrDiffSigns(BigInteger Positive, BigInteger Negative)
        {
            // Jumping over the least significant zero bits
            int iNeg = Negative.FirstNonzeroDigit;
            int iPos = Positive.FirstNonzeroDigit;
            int i;
            int limit;

            // Look if the trailing zeros of the positive will "copy" all
            // the negative digits
            if (iPos >= Negative._numberLength)
                return Negative;

            int resLength = Negative._numberLength;
            int[] resDigits = new int[resLength];

            if (iNeg < iPos)
            {
                // We know for sure that this will be the first non zero digit in the result
                for (i = iNeg; i < iPos; i++)
                    resDigits[i] = Negative._digits[i];
            }
            else if (iPos < iNeg)
            {
                i = iPos;
                resDigits[i] = -Positive._digits[i];
                limit = System.Math.Min(Positive._numberLength, iNeg);

                for (i++; i < limit; i++)
                    resDigits[i] = ~Positive._digits[i];

                if (i != Positive._numberLength)
                {
                    resDigits[i] = ~(-Negative._digits[i] | Positive._digits[i]);
                }
                else
                {
                    for (; i < iNeg; i++)
                        resDigits[i] = -1;

                    resDigits[i] = Negative._digits[i] - 1;
                }
                i++;
            }
            else
            {
                // Applying two complement to negative and to result
                i = iPos;
                resDigits[i] = -(-Negative._digits[i] | Positive._digits[i]);
                i++;
            }
            limit = System.Math.Min(Negative._numberLength, Positive._numberLength);

            // Applying two complement to negative and to result
            for (; i < limit; i++)
                resDigits[i] = Negative._digits[i] & ~Positive._digits[i];
            for (; i < Negative._numberLength; i++)
                resDigits[i] = Negative._digits[i];

            BigInteger result = new BigInteger(-1, resLength, resDigits);
            result.CutOffLeadingZeroes();

            return result;
        }
开发者ID:DeadlyEmbrace,项目名称:NTRU-NET,代码行数:64,代码来源:Logical.cs

示例15: CompleteInPlaceAdd

 /// <summary>
 /// Same as InplaceAdd(BigInteger, BigInteger), but without the restriction of non-positive values
 /// </summary>
 /// 
 /// <param name="A">The operand</param>
 /// <param name="B">The addend</param>
 internal static void CompleteInPlaceAdd(BigInteger A, BigInteger B)
 {
     if (A._sign == 0)
     {
         Array.Copy(B._digits, 0, A._digits, 0, B._numberLength);
     }
     else if (B._sign == 0)
     {
         return;
     }
     else if (A._sign == B._sign)
     {
         Add(A._digits, A._digits, A._numberLength, B._digits, B._numberLength);
     }
     else
     {
         int sign = UnsignedArraysCompare(A._digits, B._digits, A._numberLength, B._numberLength);
         if (sign > 0)
         {
             Subtract(A._digits, A._digits, A._numberLength, B._digits, B._numberLength);
         }
         else
         {
             InverseSubtract(A._digits, A._digits, A._numberLength, B._digits, B._numberLength);
             A._sign = -A._sign;
         }
     }
     A._numberLength = System.Math.Max(A._numberLength, B._numberLength) + 1;
     A.CutOffLeadingZeroes();
     A.UnCache();
 }
开发者ID:DeadlyEmbrace,项目名称:NTRU-NET,代码行数:37,代码来源:Elementary.cs


注:本文中的BigInteger.CutOffLeadingZeroes方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。