FileDocCategorySizeDatePackage
Division.javaAPI DocAndroid 1.5 API8510Wed May 06 22:41:04 BST 2009java.math

Division

public class Division extends Object
Static library that provides all operations related with division and modular arithmetic to {@link BigInteger}. Some methods are provided in both mutable and immutable way. There are several variants provided listed below:
  • Division
    • {@link BigInteger} division and remainder by {@link BigInteger}.
    • {@link BigInteger} division and remainder by {@code int}.
    • gcd between {@link BigInteger} numbers.
  • Modular arithmetic
    • Modular exponentiation between {@link BigInteger} numbers.
    • Modular inverse of a {@link BigInteger} numbers.
author
Intel Middleware Product Division
author
Instituto Tecnologico de Cordoba

Fields Summary
Constructors Summary
Methods Summary
static java.math.BigInteger[]divideAndRemainderByInteger(java.math.BigInteger val, int divisor, int divisorSign)
Computes the quotient and the remainder after a division by an {@code int} number.

return
an array of the form {@code [quotient, remainder]}.

        // BEGIN android-added
        val.establishOldRepresentation("Division.divideAndRemainderByInteger");
        // END android-added
        // res[0] is a quotient and res[1] is a remainder:
        int[] valDigits = val.digits;
        int valLen = val.numberLength;
        int valSign = val.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 };
    
static intdivideArrayByInt(int[] dest, int[] src, int srcLength, int divisor)
Divides an array by an integer value. Implements the Knuth's division algorithm. See D. Knuth, The Art of Computer Programming, vol. 2.

param
dest the quotient
param
src the dividend
param
srcLength the length of the dividend
param
divisor the divisor
return
remainder


        long rem = 0;
        long bLong = divisor & 0xffffffffL;

        for (int i = srcLength - 1; i >= 0; i--) {
            long temp = (rem << 32) | (src[i] & 0xffffffffL);
            long quot;
            if (temp >= 0) {
                quot = (temp / bLong);
                rem = (temp % bLong);
            } else {
                /*
                 * make the dividend positive shifting it right by 1 bit then
                 * get the quotient an remainder and correct them properly
                 */
                long aPos = temp >>> 1;
                long bPos = divisor >>> 1;
                quot = aPos / bPos;
                rem = aPos % bPos;
                // double the remainder and add 1 if a is odd
                rem = (rem << 1) + (temp & 1);
                if ((divisor & 1) != 0) {
                    // the divisor is odd
                    if (quot <= rem) {
                        rem -= quot;
                    } else {
                        if (quot - rem <= bLong) {
                            rem += bLong - quot;
                            quot -= 1;
                        } else {
                            rem += (bLong << 1) - quot;
                            quot -= 2;
                        }
                    }
                }
            }
            dest[i] = (int) (quot & 0xffffffffL);
        }
        return (int) rem;
    
static longdivideLongByInt(long a, int b)
Divides an unsigned long a by an unsigned int b. It is supposed that the most significant bit of b is set to 1, i.e. b < 0

param
a the dividend
param
b the divisor
return
the long value containing the unsigned integer remainder in the left half and the unsigned integer quotient in the right half

        long quot;
        long rem;
        long bLong = b & 0xffffffffL;

        if (a >= 0) {
            quot = (a / bLong);
            rem = (a % bLong);
        } else {
            /*
             * Make the dividend positive shifting it right by 1 bit then get
             * the quotient an remainder and correct them properly
             */
            long aPos = a >>> 1;
            long bPos = b >>> 1;
            quot = aPos / bPos;
            rem = aPos % bPos;
            // double the remainder and add 1 if a is odd
            rem = (rem << 1) + (a & 1);
            if ((b & 1) != 0) { // the divisor is odd
                if (quot <= rem) {
                    rem -= quot;
                } else {
                    if (quot - rem <= bLong) {
                        rem += bLong - quot;
                        quot -= 1;
                    } else {
                        rem += (bLong << 1) - quot;
                        quot -= 2;
                    }
                }
            }
        }
        return (rem << 32) | (quot & 0xffffffffL);
    
static intremainder(java.math.BigInteger dividend, int divisor)
Divides a BigInteger by a signed int and returns the remainder.

param
dividend the BigInteger to be divided. Must be non-negative.
param
divisor a signed int
return
divide % divisor

        // BEGIN android-added
        dividend.establishOldRepresentation("Division.remainder");
        // END android-added
        return remainderArrayByInt(dividend.digits, dividend.numberLength,
                divisor);
    
static intremainderArrayByInt(int[] src, int srcLength, int divisor)
Divides an array by an integer value. Implements the Knuth's division algorithm. See D. Knuth, The Art of Computer Programming, vol. 2.

param
src the dividend
param
srcLength the length of the dividend
param
divisor the divisor
return
remainder


        long result = 0;

        for (int i = srcLength - 1; i >= 0; i--) {
            long temp = (result << 32) + (src[i] & 0xffffffffL);
            long res = divideLongByInt(temp, divisor);
            result = (int) (res >> 32);
        }
        return (int) result;