Clear() public method

public Clear ( ) : void
return void
Exemplo n.º 1
0
        public static void Multiply(BigInteger bi1, ulong bi2, ref BigInteger result)
        {
            const int lastPos = maxLength - 1;

            result.Clear();
            uint x1 = (uint)bi2, x2 = (uint)(bi2 >> 32);
            int  bi2Length = x2 == 0 ? 1 : 2;

            for (int i = 0; i < bi1.Length; i++)
            {
                if (bi1.Data[i] == 0)
                {
                    continue;
                }

                ulong mcarry = 0;
                for (int j = 0, k = i; j < bi2Length; j++, k++)
                {
                    // k = i + j
                    ulong val = ((ulong)bi1.Data[i] * (ulong)(j == 0?x1:x2)) +
                                (ulong)result.Data[k] + mcarry;

                    result.Data[k] = (uint)val;
                    mcarry         = (val >> 32);
                }

                if (mcarry != 0)
                {
                    result.Data[i + bi2Length] = (uint)mcarry;
                }
            }
            result.Length = bi1.Length + bi2Length;
            if (result.Length > maxLength)
            {
                result.Length = maxLength;
            }

            while (result.Length > 1 && result.Data[result.Length - 1] == 0)
            {
                result.Length--;
            }
        }
Exemplo n.º 2
0
        public static void Multiply(BigInteger bi1, BigInteger bi2, ref BigInteger result)
        {
            const int lastPos = maxLength - 1;
            bool      bi1Neg = false, bi2Neg = false;

            // take the absolute value of the inputs
            try
            {
                if ((bi1.Data[lastPos] & 0x80000000) != 0)     // bi1 negative
                {
                    bi1Neg = true; bi1 = -bi1;
                }
                if ((bi2.Data[lastPos] & 0x80000000) != 0)     // bi2 negative
                {
                    bi2Neg = true; bi2 = -bi2;
                }
            }
            catch (Exception) { }

            result.Clear();

            // multiply the absolute values
            try
            {
                for (int i = 0; i < bi1.Length; i++)
                {
                    if (bi1.Data[i] == 0)
                    {
                        continue;
                    }

                    ulong mcarry = 0;
                    for (int j = 0, k = i; j < bi2.Length; j++, k++)
                    {
                        // k = i + j
                        ulong val = ((ulong)bi1.Data[i] * (ulong)bi2.Data[j]) +
                                    (ulong)result.Data[k] + mcarry;

                        result.Data[k] = (uint)val;
                        mcarry         = (val >> 32);
                    }

                    if (mcarry != 0)
                    {
                        result.Data[i + bi2.Length] = (uint)mcarry;
                    }
                }
            }
            catch (Exception)
            {
                throw (new ArithmeticException("Multiplication overflow."));
            }


            result.Length = bi1.Length + bi2.Length;
            if (result.Length > maxLength)
            {
                result.Length = maxLength;
            }

            while (result.Length > 1 && result.Data[result.Length - 1] == 0)
            {
                result.Length--;
            }

            // overflow check (result is -ve)
            if ((result.Data[lastPos] & 0x80000000) != 0)
            {
                if (bi1Neg != bi2Neg && result.Data[lastPos] == 0x80000000)    // different sign
                {
                    // handle the special case where multiplication produces
                    // a max negative number in 2's complement.

                    if (result.Length == 1)
                    {
                        return;
                    }
                    else
                    {
                        bool isMaxNeg = true;
                        for (int i = 0; i < result.Length - 1 && isMaxNeg; i++)
                        {
                            if (result.Data[i] != 0)
                            {
                                isMaxNeg = false;
                            }
                        }

                        if (isMaxNeg)
                        {
                            return;
                        }
                    }
                }

                throw (new ArithmeticException("Multiplication overflow."));
            }

            // if input has different signs, then result is -ve
            if (bi1Neg != bi2Neg)
            {
                result.Negative();
            }
            return;
        }
Exemplo n.º 3
0
        public static void Multiply(BigInteger bi1, BigInteger bi2,ref BigInteger result)
        {
            const int lastPos = maxLength - 1;
            bool bi1Neg = false, bi2Neg = false;

            // take the absolute value of the inputs
            try
            {
                if ((bi1.Data[lastPos] & 0x80000000) != 0)     // bi1 negative
                {
                    bi1Neg = true; bi1 = -bi1;
                }
                if ((bi2.Data[lastPos] & 0x80000000) != 0)     // bi2 negative
                {
                    bi2Neg = true; bi2 = -bi2;
                }
            }
            catch (Exception) { }

            result.Clear();

            // multiply the absolute values
            try
            {
                for (int i = 0; i < bi1.Length; i++)
                {
                    if (bi1.Data[i] == 0) continue;

                    ulong mcarry = 0;
                    for (int j = 0, k = i; j < bi2.Length; j++, k++)
                    {
                        // k = i + j
                        ulong val = ((ulong)bi1.Data[i] * (ulong)bi2.Data[j]) +
                                    (ulong)result.Data[k] + mcarry;

                        result.Data[k] = (uint)val;
                        mcarry = (val >> 32);
                    }

                    if (mcarry != 0)
                        result.Data[i + bi2.Length] = (uint)mcarry;
                }
            }
            catch (Exception)
            {
                throw (new ArithmeticException("Multiplication overflow."));
            }


            result.Length = bi1.Length + bi2.Length;
            if (result.Length > maxLength)
                result.Length = maxLength;

            while (result.Length > 1 && result.Data[result.Length - 1] == 0)
                result.Length--;

            // overflow check (result is -ve)
            if ((result.Data[lastPos] & 0x80000000) != 0)
            {
                if (bi1Neg != bi2Neg && result.Data[lastPos] == 0x80000000)    // different sign
                {
                    // handle the special case where multiplication produces
                    // a max negative number in 2's complement.

                    if (result.Length == 1)
                        return;
                    else
                    {
                        bool isMaxNeg = true;
                        for (int i = 0; i < result.Length - 1 && isMaxNeg; i++)
                        {
                            if (result.Data[i] != 0)
                                isMaxNeg = false;
                        }

                        if (isMaxNeg)
                            return;
                    }
                }

                throw (new ArithmeticException("Multiplication overflow."));
            }

            // if input has different signs, then result is -ve
            if (bi1Neg != bi2Neg) result.Negative();
            return;
        }
Exemplo n.º 4
0
        public static void Multiply(BigInteger bi1, ulong bi2, ref BigInteger result)
        {
            const int lastPos = maxLength - 1;
            result.Clear();
            uint x1= (uint)bi2, x2= (uint) (bi2 >> 32);
            int bi2Length = x2 == 0 ? 1 : 2;
            for (int i = 0; i < bi1.Length; i++)
            {
                if (bi1.Data[i] == 0) continue;

                ulong mcarry = 0;
                for (int j = 0, k = i; j < bi2Length; j++, k++)
                {
                    
                    // k = i + j
                    ulong val = ((ulong)bi1.Data[i] * (ulong)(j==0?x1:x2)) +
                                (ulong)result.Data[k] + mcarry;

                    result.Data[k] = (uint)val;
                    mcarry = (val >> 32);
                }

                if (mcarry != 0)
                    result.Data[i + bi2Length] = (uint)mcarry;
            }
            result.Length = bi1.Length + bi2Length;
            if (result.Length > maxLength)
                result.Length = maxLength;

            while (result.Length > 1 && result.Data[result.Length - 1] == 0)
                result.Length--;
        }