Exemplo n.º 1
0
        /// <summary>
        /// Performs the same as GcdBinary(BigInteger, BigInteger)}, but with numbers of 63 bits,
        /// represented in positives values of long type.
        /// </summary>
        ///
        /// <param name="X">A positive number</param>
        /// <param name="Y">A positive number></param>
        ///
        /// <returns>Returns <c>Gcd(X, Y)</c></returns>
        internal static long GcdBinary(long X, long Y)
        {
            // (op1 > 0) and (op2 > 0)
            int lsb1      = IntUtils.NumberOfTrailingZeros(X);
            int lsb2      = IntUtils.NumberOfTrailingZeros(Y);
            int pow2Count = System.Math.Min(lsb1, lsb2);

            if (lsb1 != 0)
            {
                X = IntUtils.URShift(X, lsb1);
            }
            if (lsb2 != 0)
            {
                Y = IntUtils.URShift(Y, lsb2);
            }

            do
            {
                if (X >= Y)
                {
                    X -= Y;
                    X  = IntUtils.URShift(X, IntUtils.NumberOfTrailingZeros(X));
                }
                else
                {
                    Y -= X;
                    Y  = IntUtils.URShift(Y, IntUtils.NumberOfTrailingZeros(Y));
                }
            } while (X != 0);

            return(Y << pow2Count);
        }