java.math
Class BigInteger

java.lang.Object
  |
  +--java.lang.Number
        |
        +--java.math.BigInteger
All Implemented Interfaces:
Comparable, Serializable

public class BigInteger
extends Number
implements Comparable

Safe:

See Also:
Serialized Form

Field Summary
private  int bitCount
          The bitCount of this BigInteger, as returned by bitCount(), or -1 (either value is acceptable).
private  int bitLength
          The bitLength of this BigInteger, as returned by bitLength(), or -1 (either value is acceptable).
private static long[] bitsPerDigit
           
(package private) static int[] bnExpModThreshTable
           
private static int[] digitsPerInt
           
private static int[] digitsPerLong
           
private  int firstNonzeroByteNum
          The index of the lowest-order byte in the magnitude of this BigInteger that contains a nonzero byte, or -2 (either value is acceptable).
private  int firstNonzeroIntNum
          The index of the lowest-order int in the magnitude of this BigInteger that contains a nonzero int, or -2 (either value is acceptable).
private static int[] intRadix
           
private static long LONG_MASK
          This mask is used to obtain the value of an int as if it were unsigned.
private static BigInteger[] longRadix
           
private  int lowestSetBit
          The lowest set bit of this BigInteger, as returned by getLowestSetBit(), or -2 (either value is acceptable).
(package private)  int[] mag
          The magnitude of this BigInteger, in big-endian order: the zeroth element of this array is the most-significant int of the magnitude.
private static int MAX_CONSTANT
          Initialize static constant array when class is loaded.
private static BigInteger[] negConst
           
static BigInteger ONE
          Enabled: The BigInteger constant one.
private static BigInteger[] posConst
           
private static ObjectStreamField[] serialPersistentFields
          Serializable fields for BigInteger.
private static long serialVersionUID
          use serialVersionUID from JDK 1.1.
(package private)  int signum
          The signum of this BigInteger: -1 for negative, 0 for zero, or 1 for positive.
private static BigInteger SMALL_PRIME_PRODUCT
           
private static int SMALL_PRIME_THRESHOLD
           
(package private) static byte[] trailingZeroTable
           
private static BigInteger TWO
          The BigInteger constant two.
static BigInteger ZERO
          Enabled: The BigInteger constant zero.
private static String[] zeros
           
 
Constructor Summary
  BigInteger(byte[] val)
          Enabled: Translates a byte array containing the two's-complement binary representation of a BigInteger into a BigInteger.
private BigInteger(byte[] magnitude, int signum)
          This private constructor is for internal use and assumes that its arguments are correct.
(package private) BigInteger(char[] val)
           
private BigInteger(int[] val)
          This private constructor translates an int array containing the two's-complement binary representation of a BigInteger into a BigInteger.
private BigInteger(int[] magnitude, int signum)
          This private constructor differs from its public cousin with the arguments reversed in two ways: it assumes that its arguments are correct, and it doesn't copy the magnitude array.
  BigInteger(int signum, byte[] magnitude)
          Enabled: Translates the sign-magnitude representation of a BigInteger into a BigInteger.
private BigInteger(int signum, int[] magnitude)
          A constructor for internal use that translates the sign-magnitude representation of a BigInteger into a BigInteger.
  BigInteger(int bitLength, int certainty, Random rnd)
          Enabled: Constructs a randomly generated positive BigInteger that is probably prime, with the specified bitLength.
  BigInteger(int numBits, Random rnd)
          Enabled: Constructs a randomly generated BigInteger, uniformly distributed over the range 0 to (2numBits - 1), inclusive.
private BigInteger(long val)
          Constructs a BigInteger with the specified value, which may not be zero.
(package private) BigInteger(MutableBigInteger val, int sign)
          This private constructor is for internal use in converting from a MutableBigInteger object into a BigInteger.
  BigInteger(String val)
          Enabled: Translates the decimal String representation of a BigInteger into a BigInteger.
  BigInteger(String val, int radix)
          Enabled: Translates the String representation of a BigInteger in the specified radix into a BigInteger.
 
Method Summary
 boolean aboveZero()
          Added: Used in the expansion of E's ">" operator
 BigInteger abs()
          Enabled: Returns a BigInteger whose value is the absolute value of this BigInteger.
 BigInteger add(BigInteger val)
          Enabled: Returns a BigInteger whose value is (this + val).
private static int[] add(int[] x, int[] y)
          Adds the contents of the int arrays x and y.
(package private) static int addOne(int[] a, int offset, int mlen, int carry)
          Add one word to the number a mlen words into a.
 BigInteger and(BigInteger val)
          Enabled: Returns a BigInteger whose value is (this & val).
 BigInteger andNot(BigInteger val)
          Suppressed: Returns a BigInteger whose value is (this & ~val).
 double approxDivide(double arg)
          Added: Always gives back a Double This corresponds to the Java floating-point "/" operator and the E "/" operator.
 char asChar()
          Added: Returns the Unicode character with this character code.
 double asFloat64()
          Added: Returns the "best" IEEE double precision floating point equivalent to this number.
 boolean atLeastZero()
          Added: Used in the expansion of E's ">=" operator
 boolean atMostZero()
          Added: Used in the expansion of E's "<=" operator
 boolean belowZero()
          Added: Used in the expansion of E's "<" operator
(package private) static int bitCnt(int val)
           
 int bitCount()
          Enabled: Returns the number of bits in the two's complement representation of this BigInteger that differ from its sign bit.
(package private) static int bitLen(int w)
          bitLen(val) is the number of bits in val.
 int bitLength()
          Enabled: Returns the number of bits in the minimal two's-complement representation of this BigInteger, excluding a sign bit.
private static int bitLength(int[] val, int len)
          Calculate bitlength of contents of the first len elements an int array, assuming there are no leading zero ints.
 BigInteger butNot(BigInteger arg)
          Added: In E, andNot() is called butNot()
 BigInteger ceil()
          Added: Defined both here and in DoubleSugar, so you can use this to get a ceil'ed integer no matter what kind of number you start with.
 BigInteger clearBit(int n)
          Enabled: Returns a BigInteger whose value is equivalent to this BigInteger with the designated bit cleared.
 int compareTo(BigInteger val)
          Suppressed: Compares this BigInteger with the specified BigInteger.
 int compareTo(Object o)
          Enabled: Compares this BigInteger with the specified Object.
 int compareTo(Object o)
          Added: Overrides the inherited 'compareTo' to coerce 'o' to a BigInteger
 BigInteger complement()
          Deprecated. Use not() instead.
 BigInteger cryptoHash()
          Added: Actually, a SHA hash
private static void destructiveMulAdd(int[] x, int y, int z)
           
 BigInteger divide(BigInteger val)
          Enabled: Returns a BigInteger whose value is (this / val).
 BigInteger[] divideAndRemainder(BigInteger val)
          Enabled: Returns an array of two BigIntegers containing (this / val) followed by (this % val).
 double doubleValue()
          Enabled: Converts this BigInteger to a double.
 boolean equals(Object x)
          Suppressed: Compares this BigInteger with the specified Object for equality.
private  int firstNonzeroIntNum()
          Returns the index of the int that contains the first nonzero int in the little-endian binary representation of the magnitude (int 0 is the least significant).
 BigInteger flipBit(int n)
          Enabled: Returns a BigInteger whose value is equivalent to this BigInteger with the designated bit flipped.
 float floatValue()
          Enabled: Converts this BigInteger to a float.
 BigInteger floor()
          Added: Defined both here and in DoubleSugar, so you can use this to get a floor'ed integer no matter what kind of number you start with.
 BigInteger floorDivide(BigInteger arg)
          Added: Always gives an integer, resulting from rounding towards negative infinity, ie, flooring.
static BigInteger fromString64(String s)
          Added: Convert a base 64 string to an integer.
 BigInteger gcd(BigInteger val)
          Enabled: Returns a BigInteger whose value is the greatest common divisor of abs(this) and abs(val).
private  int getInt(int n)
          Returns the specified int of the little-endian two's complement representation (int 0 is the least significant).
 int getLowestSetBit()
          Enabled: Returns the index of the rightmost (lowest-order) one bit in this BigInteger (the number of zero bits to the right of the rightmost one bit).
 int hashCode()
          Suppressed: Returns the hash code for this BigInteger.
private static int intArrayCmp(int[] arg1, int[] arg2)
           
private static int intArrayCmpToLen(int[] arg1, int[] arg2, int len)
           
private  int intLength()
          Returns the length of the two's complement representation in ints, including space for at least one sign bit.
 int intValue()
          Enabled: Converts this BigInteger to an int.
 boolean isNaN()
          Added: All integers are numbers, so this is always false
 boolean isProbablePrime(int certainty)
          Suppressed: Returns true if this BigInteger is probably prime, false if it's definitely composite.
 boolean isZero()
          Added: Used for arithmetic equality
(package private)  int jacobiSymbol(int p, BigInteger n)
          Computes Jacobi(p,n).
(package private)  int[] javaIncrement(int[] val)
           
private static BigInteger largePrime(int bitLength, int certainty, Random rnd)
          Find a random number of the specified bitLength that is probably prime.
private static int[] leftShift(int[] a, int len, int n)
          Left shift int array a up to len by n bits.
 long longValue()
          Enabled: Converts this BigInteger to a long.
private static BigInteger lucasLehmerSequence(int z, BigInteger k, BigInteger n)
           
private  byte[] magSerializedForm()
          Returns the mag array as an array of bytes.
private static int[] makePositive(byte[] a)
          Takes an array a representing a negative 2's-complement number and returns the minimal (no leading zero bytes) unsigned whose value is -a.
private static int[] makePositive(int[] a)
          Takes an array a representing a negative 2's-complement number and returns the minimal (no leading zero ints) unsigned whose value is -a.
 BigInteger max(BigInteger val)
          Enabled: Returns the maximum of this BigInteger and val.
 BigInteger min(BigInteger val)
          Enabled: Returns the minimum of this BigInteger and val.
 BigInteger mod(BigInteger m)
          Enabled: Returns a BigInteger whose value is (this mod m).
 BigInteger mod(BigInteger arg)
          Added: Remainder from the floorDivide operation.
private  BigInteger mod2(int p)
          Returns a BigInteger whose value is this mod(2**p).
 BigInteger modInverse(BigInteger m)
          Enabled: Returns a BigInteger whose value is (this-1 mod m).
 BigInteger modPow(BigInteger exponent, BigInteger m)
          Enabled: Returns a BigInteger whose value is (thisexponent mod m).
private  BigInteger modPow2(BigInteger exponent, int p)
          Returns a BigInteger whose value is (this ** exponent) mod (2**p)
private static int[] montReduce(int[] n, int[] mod, int mlen, int inv)
          Montgomery reduce n, modulo mod.
(package private) static int mulAdd(int[] out, int[] in, int offset, int len, int k)
          Multiply an array by one word k and add to result, return the carry
 BigInteger multiply(BigInteger val)
          Enabled: Returns a BigInteger whose value is (this * val).
private  int[] multiplyToLen(int[] x, int xlen, int[] y, int ylen, int[] z)
          Multiplies int arrays x and y to the specified lengths and places the result into z.
 BigInteger negate()
          Enabled: Returns a BigInteger whose value is (-this).
 BigInteger next()
          Added: The next higher integer
 BigInteger not()
          Enabled: Returns a BigInteger whose value is (~this).
private  BigInteger oddModPow(BigInteger y, BigInteger z)
          Returns a BigInteger whose value is x to the power of y mod z.
 BigInteger or(BigInteger val)
          Enabled: Returns a BigInteger whose value is (this | val).
private  int parseInt(char[] source, int start, int end)
           
private  boolean passesLucasLehmer()
          Returns true iff this BigInteger is a Lucas-Lehmer probable prime.
private  boolean passesMillerRabin(int iterations)
          Returns true iff this BigInteger passes the specified number of Miller-Rabin tests.
 BigInteger pow(int exponent)
          Enabled: Returns a BigInteger whose value is (thisexponent).
 BigInteger previous()
          Added: The next lower integer
(package private)  boolean primeToCertainty(int certainty)
          Returns true if this BigInteger is probably prime, false if it's definitely composite.
(package private) static void primitiveLeftShift(int[] a, int len, int n)
           
(package private) static void primitiveRightShift(int[] a, int len, int n)
           
static BigInteger probablePrime(int bitLength, Random rnd)
          Suppressed: Returns a positive BigInteger that is probably prime, with the specified bitLength.
private static byte[] randomBits(int numBits, Random rnd)
           
private  void readObject(ObjectInputStream s)
          Reconstitute the BigInteger instance from a stream (that is, deserialize it).
 BigInteger remainder(BigInteger val)
          Enabled: Returns a BigInteger whose value is (this % val).
 BigInteger round()
          Added: Defined both here and in DoubleSugar, so you can use this to get round'ed integer no matter what kind of number you start with.
 BigInteger setBit(int n)
          Enabled: Returns a BigInteger whose value is equivalent to this BigInteger with the designated bit set.
 BigInteger shiftLeft(int n)
          Enabled: Returns a BigInteger whose value is (this << n).
 BigInteger shiftRight(int n)
          Enabled: Returns a BigInteger whose value is (this >> n).
private  int signBit()
           
private  int signInt()
           
 int signum()
          Enabled: Returns the signum function of this BigInteger.
private static BigInteger smallPrime(int bitLength, int certainty, Random rnd)
          Find a random number of the specified bitLength that is probably prime.
private  BigInteger square()
          Returns a BigInteger whose value is (this2).
private static int[] squareToLen(int[] x, int len, int[] z)
          Squares the contents of the int array x.
private static int[] stripLeadingZeroBytes(byte[] a)
          Returns a copy of the input array stripped of any leading zero bytes.
private static int[] stripLeadingZeroInts(int[] val)
          Returns a copy of the input array stripped of any leading zero bytes.
private static int subN(int[] a, int[] b, int len)
          Subtracts two numbers of same length, returning borrow.
 BigInteger subtract(BigInteger val)
          Enabled: Returns a BigInteger whose value is (this - val).
private static int[] subtract(int[] big, int[] little)
          Subtracts the contents of the second int arrays (little) from the first (big).
 boolean testBit(int n)
          Enabled: Returns true if and only if the designated bit is set.
 byte[] toByteArray()
          Enabled: Returns a byte array containing the two's-complement representation of this BigInteger.
 String toString()
          Suppressed: Returns the decimal String representation of this BigInteger.
 String toString(int radix)
          Enabled: Returns the String representation of this BigInteger in the given radix.
 String toString64()
          Added: Convert an integer to a base 64 string, with an optional leading minus sign.
(package private) static int trailingZeroCnt(int val)
           
 BigInteger truncate()
          Added: Defined both here and in DoubleSugar, so you can use this to get truncate'd integer no matter what kind of number you start with.
 BigInteger truncDivide(BigInteger arg)
          Added: Always gives an integer resulting from rounding towards zero, ie, truncating.
private static int[] trustedStripLeadingZeroInts(int[] val)
          Returns the input array stripped of any leading zero bytes.
private static BigInteger valueOf(int[] val)
          Returns a BigInteger with the given two's complement representation.
static BigInteger valueOf(long val)
          Enabled: Returns a BigInteger whose value is equal to that of the specified long.
private  void writeObject(ObjectOutputStream s)
          Save the BigInteger instance to a stream.
 BigInteger xor(BigInteger val)
          Enabled: Returns a BigInteger whose value is (this ^ val).
 
Methods inherited from class java.lang.Number
byteValue, shortValue
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

signum

int signum
The signum of this BigInteger: -1 for negative, 0 for zero, or 1 for positive. Note that the BigInteger zero must have a signum of 0. This is necessary to ensures that there is exactly one representation for each BigInteger value.


mag

int[] mag
The magnitude of this BigInteger, in big-endian order: the zeroth element of this array is the most-significant int of the magnitude. The magnitude must be "minimal" in that the most-significant int (mag[0]) must be non-zero. This is necessary to ensure that there is exactly one representation for each BigInteger value. Note that this implies that the BigInteger zero has a zero-length mag array.


bitCount

private int bitCount
The bitCount of this BigInteger, as returned by bitCount(), or -1 (either value is acceptable).

See Also:
bitCount

bitLength

private int bitLength
The bitLength of this BigInteger, as returned by bitLength(), or -1 (either value is acceptable).

See Also:
bitLength

lowestSetBit

private int lowestSetBit
The lowest set bit of this BigInteger, as returned by getLowestSetBit(), or -2 (either value is acceptable).

See Also:
getLowestSetBit()

firstNonzeroByteNum

private int firstNonzeroByteNum
The index of the lowest-order byte in the magnitude of this BigInteger that contains a nonzero byte, or -2 (either value is acceptable). The least significant byte has int-number 0, the next byte in order of increasing significance has byte-number 1, and so forth.


firstNonzeroIntNum

private int firstNonzeroIntNum
The index of the lowest-order int in the magnitude of this BigInteger that contains a nonzero int, or -2 (either value is acceptable). The least significant int has int-number 0, the next int in order of increasing significance has int-number 1, and so forth.


LONG_MASK

private static final long LONG_MASK
This mask is used to obtain the value of an int as if it were unsigned.


bitsPerDigit

private static long[] bitsPerDigit

SMALL_PRIME_THRESHOLD

private static final int SMALL_PRIME_THRESHOLD

SMALL_PRIME_PRODUCT

private static final BigInteger SMALL_PRIME_PRODUCT

MAX_CONSTANT

private static final int MAX_CONSTANT
Initialize static constant array when class is loaded.


posConst

private static BigInteger[] posConst

negConst

private static BigInteger[] negConst

ZERO

public static final BigInteger ZERO
Enabled: The BigInteger constant zero.

Since:
1.2

ONE

public static final BigInteger ONE
Enabled: The BigInteger constant one.

Since:
1.2

TWO

private static final BigInteger TWO
The BigInteger constant two. (Not exported.)


bnExpModThreshTable

static int[] bnExpModThreshTable

trailingZeroTable

static final byte[] trailingZeroTable

zeros

private static String[] zeros

digitsPerLong

private static int[] digitsPerLong

longRadix

private static BigInteger[] longRadix

digitsPerInt

private static int[] digitsPerInt

intRadix

private static int[] intRadix

serialVersionUID

private static final long serialVersionUID
use serialVersionUID from JDK 1.1. for interoperability


serialPersistentFields

private static final ObjectStreamField[] serialPersistentFields
Serializable fields for BigInteger.

Constructor Detail

BigInteger

public BigInteger(byte[] val)
Enabled: Translates a byte array containing the two's-complement binary representation of a BigInteger into a BigInteger. The input array is assumed to be in big-endian byte-order: the most significant byte is in the zeroth element.

Parameters:
val - big-endian two's-complement binary representation of BigInteger.
Throws:
NumberFormatException - val is zero bytes long.

BigInteger

private BigInteger(int[] val)
This private constructor translates an int array containing the two's-complement binary representation of a BigInteger into a BigInteger. The input array is assumed to be in big-endian int-order: the most significant int is in the zeroth element.


BigInteger

public BigInteger(int signum,
                  byte[] magnitude)
Enabled: Translates the sign-magnitude representation of a BigInteger into a BigInteger. The sign is represented as an integer signum value: -1 for negative, 0 for zero, or 1 for positive. The magnitude is a byte array in big-endian byte-order: the most significant byte is in the zeroth element. A zero-length magnitude array is permissible, and will result in in a BigInteger value of 0, whether signum is -1, 0 or 1.

Parameters:
signum - signum of the number (-1 for negative, 0 for zero, 1 for positive).
magnitude - big-endian binary representation of the magnitude of the number.
Throws:
NumberFormatException - signum is not one of the three legal values (-1, 0, and 1), or signum is 0 and magnitude contains one or more non-zero bytes.

BigInteger

private BigInteger(int signum,
                   int[] magnitude)
A constructor for internal use that translates the sign-magnitude representation of a BigInteger into a BigInteger. It checks the arguments and copies the magnitude so this constructor would be safe for external use.


BigInteger

public BigInteger(String val,
                  int radix)
Enabled: Translates the String representation of a BigInteger in the specified radix into a BigInteger. The String representation consists of an optional minus sign followed by a sequence of one or more digits in the specified radix. The character-to-digit mapping is provided by Character.digit. The String may not contain any extraneous characters (whitespace, for example).

Parameters:
val - String representation of BigInteger.
radix - radix to be used in interpreting val.
Throws:
NumberFormatException - val is not a valid representation of a BigInteger in the specified radix, or radix is outside the range from Character.MIN_RADIX to Character.MAX_RADIX, inclusive.
See Also:
Character.digit(char, int)

BigInteger

BigInteger(char[] val)

BigInteger

public BigInteger(String val)
Enabled: Translates the decimal String representation of a BigInteger into a BigInteger. The String representation consists of an optional minus sign followed by a sequence of one or more decimal digits. The character-to-digit mapping is provided by Character.digit. The String may not contain any extraneous characters (whitespace, for example).

Parameters:
val - decimal String representation of BigInteger.
Throws:
NumberFormatException - val is not a valid representation of a BigInteger.
See Also:
Character.digit(char, int)

BigInteger

public BigInteger(int numBits,
                  Random rnd)
Enabled: Constructs a randomly generated BigInteger, uniformly distributed over the range 0 to (2numBits - 1), inclusive. The uniformity of the distribution assumes that a fair source of random bits is provided in rnd. Note that this constructor always constructs a non-negative BigInteger.

Parameters:
numBits - maximum bitLength of the new BigInteger.
rnd - source of randomness to be used in computing the new BigInteger.
Throws:
IllegalArgumentException - numBits is negative.
See Also:
bitLength

BigInteger

public BigInteger(int bitLength,
                  int certainty,
                  Random rnd)
Enabled: Constructs a randomly generated positive BigInteger that is probably prime, with the specified bitLength.

It is recommended that the probablePrime method be used in preference to this constructor unless there is a compelling need to specify a certainty.

Parameters:
bitLength - bitLength of the returned BigInteger.
certainty - a measure of the uncertainty that the caller is willing to tolerate. The probability that the new BigInteger represents a prime number will exceed (1 - 1/2certainty). The execution time of this constructor is proportional to the value of this parameter.
rnd - source of random bits used to select candidates to be tested for primality.
Throws:
ArithmeticException - bitLength < 2.
See Also:
bitLength

BigInteger

private BigInteger(int[] magnitude,
                   int signum)
This private constructor differs from its public cousin with the arguments reversed in two ways: it assumes that its arguments are correct, and it doesn't copy the magnitude array.


BigInteger

private BigInteger(byte[] magnitude,
                   int signum)
This private constructor is for internal use and assumes that its arguments are correct.


BigInteger

BigInteger(MutableBigInteger val,
           int sign)
This private constructor is for internal use in converting from a MutableBigInteger object into a BigInteger.


BigInteger

private BigInteger(long val)
Constructs a BigInteger with the specified value, which may not be zero.

Method Detail

parseInt

private int parseInt(char[] source,
                     int start,
                     int end)

destructiveMulAdd

private static void destructiveMulAdd(int[] x,
                                      int y,
                                      int z)

randomBits

private static byte[] randomBits(int numBits,
                                 Random rnd)

probablePrime

public static BigInteger probablePrime(int bitLength,
                                       Random rnd)
Suppressed: Returns a positive BigInteger that is probably prime, with the specified bitLength. The probability that a BigInteger returned by this method is composite does not exceed 2-100.

Parameters:
bitLength - bitLength of the returned BigInteger.
rnd - source of random bits used to select candidates to be tested for primality.
Returns:
a BigInteger of bitLength bits that is probably prime
Throws:
ArithmeticException - bitLength < 2.
See Also:
bitLength

smallPrime

private static BigInteger smallPrime(int bitLength,
                                     int certainty,
                                     Random rnd)
Find a random number of the specified bitLength that is probably prime. This method is used for smaller primes, its performance degrades on larger bitlengths. This method assumes bitLength > 1.


largePrime

private static BigInteger largePrime(int bitLength,
                                     int certainty,
                                     Random rnd)
Find a random number of the specified bitLength that is probably prime. This method is more appropriate for larger bitlengths since it uses a sieve to eliminate most composites before using a more expensive test.


primeToCertainty

boolean primeToCertainty(int certainty)
Returns true if this BigInteger is probably prime, false if it's definitely composite. This method assumes bitLength > 2.

Parameters:
certainty - a measure of the uncertainty that the caller is willing to tolerate: if the call returns true the probability that this BigInteger is prime exceeds (1 - 1/2certainty). The execution time of this method is proportional to the value of this parameter.
Returns:
true if this BigInteger is probably prime, false if it's definitely composite.

passesLucasLehmer

private boolean passesLucasLehmer()
Returns true iff this BigInteger is a Lucas-Lehmer probable prime. The following assumptions are made: This BigInteger is a positive, odd number.


jacobiSymbol

int jacobiSymbol(int p,
                 BigInteger n)
Computes Jacobi(p,n). Assumes n is positive, odd.


lucasLehmerSequence

private static BigInteger lucasLehmerSequence(int z,
                                              BigInteger k,
                                              BigInteger n)

passesMillerRabin

private boolean passesMillerRabin(int iterations)
Returns true iff this BigInteger passes the specified number of Miller-Rabin tests. This test is taken from the DSA spec (NIST FIPS 186-2). The following assumptions are made: This BigInteger is a positive, odd number greater than 2. iterations<=50.


valueOf

public static BigInteger valueOf(long val)
Enabled: Returns a BigInteger whose value is equal to that of the specified long. This "static factory method" is provided in preference to a (long) constructor because it allows for reuse of frequently used BigIntegers.

Parameters:
val - value of the BigInteger to return.
Returns:
a BigInteger with the specified value.

valueOf

private static BigInteger valueOf(int[] val)
Returns a BigInteger with the given two's complement representation. Assumes that the input array will not be modified (the returned BigInteger will reference the input array if feasible).


add

public BigInteger add(BigInteger val)
Enabled: Returns a BigInteger whose value is (this + val).

Parameters:
val - value to be added to this BigInteger.
Returns:
this + val

add

private static int[] add(int[] x,
                         int[] y)
Adds the contents of the int arrays x and y. This method allocates a new int array to hold the answer and returns a reference to that array.


subtract

public BigInteger subtract(BigInteger val)
Enabled: Returns a BigInteger whose value is (this - val).

Parameters:
val - value to be subtracted from this BigInteger.
Returns:
this - val

subtract

private static int[] subtract(int[] big,
                              int[] little)
Subtracts the contents of the second int arrays (little) from the first (big). The first int array (big) must represent a larger number than the second. This method allocates the space necessary to hold the answer.


multiply

public BigInteger multiply(BigInteger val)
Enabled: Returns a BigInteger whose value is (this * val).

Parameters:
val - value to be multiplied by this BigInteger.
Returns:
this * val

multiplyToLen

private int[] multiplyToLen(int[] x,
                            int xlen,
                            int[] y,
                            int ylen,
                            int[] z)
Multiplies int arrays x and y to the specified lengths and places the result into z.


square

private BigInteger square()
Returns a BigInteger whose value is (this2).

Returns:
this2

squareToLen

private static final int[] squareToLen(int[] x,
                                       int len,
                                       int[] z)
Squares the contents of the int array x. The result is placed into the int array z. The contents of x are not changed.


divide

public BigInteger divide(BigInteger val)
Enabled: Returns a BigInteger whose value is (this / val).

Parameters:
val - value by which this BigInteger is to be divided.
Returns:
this / val
Throws:
ArithmeticException - val==0

divideAndRemainder

public BigInteger[] divideAndRemainder(BigInteger val)
Enabled: Returns an array of two BigIntegers containing (this / val) followed by (this % val).

Parameters:
val - value by which this BigInteger is to be divided, and the remainder computed.
Returns:
an array of two BigIntegers: the quotient (this / val) is the initial element, and the remainder (this % val) is the final element.
Throws:
ArithmeticException - val==0

remainder

public BigInteger remainder(BigInteger val)
Enabled: Returns a BigInteger whose value is (this % val).

Parameters:
val - value by which this BigInteger is to be divided, and the remainder computed.
Returns:
this % val
Throws:
ArithmeticException - val==0

pow

public BigInteger pow(int exponent)
Enabled: Returns a BigInteger whose value is (thisexponent). Note that exponent is an integer rather than a BigInteger.

Parameters:
exponent - exponent to which this BigInteger is to be raised.
Returns:
thisexponent
Throws:
ArithmeticException - exponent is negative. (This would cause the operation to yield a non-integer value.)

gcd

public BigInteger gcd(BigInteger val)
Enabled: Returns a BigInteger whose value is the greatest common divisor of abs(this) and abs(val). Returns 0 if this==0 && val==0.

Parameters:
val - value with with the GCD is to be computed.
Returns:
GCD(abs(this), abs(val))

leftShift

private static int[] leftShift(int[] a,
                               int len,
                               int n)
Left shift int array a up to len by n bits. Returns the array that results from the shift since space may have to be reallocated.


primitiveRightShift

static void primitiveRightShift(int[] a,
                                int len,
                                int n)

primitiveLeftShift

static void primitiveLeftShift(int[] a,
                               int len,
                               int n)

bitLength

private static int bitLength(int[] val,
                             int len)
Calculate bitlength of contents of the first len elements an int array, assuming there are no leading zero ints.


abs

public BigInteger abs()
Enabled: Returns a BigInteger whose value is the absolute value of this BigInteger.

Returns:
abs(this)

negate

public BigInteger negate()
Enabled: Returns a BigInteger whose value is (-this).

Returns:
-this

signum

public int signum()
Enabled: Returns the signum function of this BigInteger.

Returns:
-1, 0 or 1 as the value of this BigInteger is negative, zero or positive.

mod

public BigInteger mod(BigInteger m)
Enabled: Returns a BigInteger whose value is (this mod m). This method differs from remainder in that it always returns a non-negative BigInteger.

Parameters:
m - the modulus.
Returns:
this mod m
Throws:
ArithmeticException - m <= 0
See Also:
remainder(java.math.BigInteger)

modPow

public BigInteger modPow(BigInteger exponent,
                         BigInteger m)
Enabled: Returns a BigInteger whose value is (thisexponent mod m). (Unlike pow, this method permits negative exponents.)

Parameters:
exponent - the exponent.
m - the modulus.
Returns:
thisexponent mod m
Throws:
ArithmeticException - m <= 0
See Also:
modInverse(java.math.BigInteger)

oddModPow

private BigInteger oddModPow(BigInteger y,
                             BigInteger z)
Returns a BigInteger whose value is x to the power of y mod z. Assumes: z is odd && x < z.


montReduce

private static int[] montReduce(int[] n,
                                int[] mod,
                                int mlen,
                                int inv)
Montgomery reduce n, modulo mod. This reduces modulo mod and divides by 2^(32*mlen). Adapted from Colin Plumb's C library.


intArrayCmpToLen

private static int intArrayCmpToLen(int[] arg1,
                                    int[] arg2,
                                    int len)

subN

private static int subN(int[] a,
                        int[] b,
                        int len)
Subtracts two numbers of same length, returning borrow.


mulAdd

static int mulAdd(int[] out,
                  int[] in,
                  int offset,
                  int len,
                  int k)
Multiply an array by one word k and add to result, return the carry


addOne

static int addOne(int[] a,
                  int offset,
                  int mlen,
                  int carry)
Add one word to the number a mlen words into a. Return the resulting carry.


modPow2

private BigInteger modPow2(BigInteger exponent,
                           int p)
Returns a BigInteger whose value is (this ** exponent) mod (2**p)


mod2

private BigInteger mod2(int p)
Returns a BigInteger whose value is this mod(2**p). Assumes that this BigInteger >= 0 and p > 0.


modInverse

public BigInteger modInverse(BigInteger m)
Enabled: Returns a BigInteger whose value is (this-1 mod m).

Parameters:
m - the modulus.
Returns:
this-1 mod m.
Throws:
ArithmeticException - m <= 0, or this BigInteger has no multiplicative inverse mod m (that is, this BigInteger is not relatively prime to m).

shiftLeft

public BigInteger shiftLeft(int n)
Enabled: Returns a BigInteger whose value is (this << n). The shift distance, n, may be negative, in which case this method performs a right shift. (Computes floor(this * 2n).)

Parameters:
n - shift distance, in bits.
Returns:
this << n
See Also:
shiftRight(int)

shiftRight

public BigInteger shiftRight(int n)
Enabled: Returns a BigInteger whose value is (this >> n). Sign extension is performed. The shift distance, n, may be negative, in which case this method performs a left shift. (Computes floor(this / 2n).)

Parameters:
n - shift distance, in bits.
Returns:
this >> n
See Also:
shiftLeft(int)

javaIncrement

int[] javaIncrement(int[] val)

and

public BigInteger and(BigInteger val)
Enabled: Returns a BigInteger whose value is (this & val). (This method returns a negative BigInteger if and only if this and val are both negative.)

Parameters:
val - value to be AND'ed with this BigInteger.
Returns:
this & val

or

public BigInteger or(BigInteger val)
Enabled: Returns a BigInteger whose value is (this | val). (This method returns a negative BigInteger if and only if either this or val is negative.)

Parameters:
val - value to be OR'ed with this BigInteger.
Returns:
this | val

xor

public BigInteger xor(BigInteger val)
Enabled: Returns a BigInteger whose value is (this ^ val). (This method returns a negative BigInteger if and only if exactly one of this and val are negative.)

Parameters:
val - value to be XOR'ed with this BigInteger.
Returns:
this ^ val

not

public BigInteger not()
Enabled: Returns a BigInteger whose value is (~this). (This method returns a negative value if and only if this BigInteger is non-negative.)

Returns:
~this

andNot

public BigInteger andNot(BigInteger val)
Suppressed: Returns a BigInteger whose value is (this & ~val). This method, which is equivalent to and(val.not()), is provided as a convenience for masking operations. (This method returns a negative BigInteger if and only if this is negative and val is positive.)

Parameters:
val - value to be complemented and AND'ed with this BigInteger.
Returns:
this & ~val

testBit

public boolean testBit(int n)
Enabled: Returns true if and only if the designated bit is set. (Computes ((this & (1<<n)) != 0).)

Parameters:
n - index of bit to test.
Returns:
true if and only if the designated bit is set.
Throws:
ArithmeticException - n is negative.

setBit

public BigInteger setBit(int n)
Enabled: Returns a BigInteger whose value is equivalent to this BigInteger with the designated bit set. (Computes (this | (1<<n)).)

Parameters:
n - index of bit to set.
Returns:
this | (1<<n)
Throws:
ArithmeticException - n is negative.

clearBit

public BigInteger clearBit(int n)
Enabled: Returns a BigInteger whose value is equivalent to this BigInteger with the designated bit cleared. (Computes (this & ~(1<<n)).)

Parameters:
n - index of bit to clear.
Returns:
this & ~(1<<n)
Throws:
ArithmeticException - n is negative.

flipBit

public BigInteger flipBit(int n)
Enabled: Returns a BigInteger whose value is equivalent to this BigInteger with the designated bit flipped. (Computes (this ^ (1<<n)).)

Parameters:
n - index of bit to flip.
Returns:
this ^ (1<<n)
Throws:
ArithmeticException - n is negative.

getLowestSetBit

public int getLowestSetBit()
Enabled: Returns the index of the rightmost (lowest-order) one bit in this BigInteger (the number of zero bits to the right of the rightmost one bit). Returns -1 if this BigInteger contains no one bits. (Computes (this==0? -1 : log2(this & -this)).)

Returns:
index of the rightmost one bit in this BigInteger.

bitLength

public int bitLength()
Enabled: Returns the number of bits in the minimal two's-complement representation of this BigInteger, excluding a sign bit. For positive BigIntegers, this is equivalent to the number of bits in the ordinary binary representation. (Computes (ceil(log2(this < 0 ? -this : this+1))).)

Returns:
number of bits in the minimal two's-complement representation of this BigInteger, excluding a sign bit.

bitLen

static int bitLen(int w)
bitLen(val) is the number of bits in val.


bitCount

public int bitCount()
Enabled: Returns the number of bits in the two's complement representation of this BigInteger that differ from its sign bit. This method is useful when implementing bit-vector style sets atop BigIntegers.

Returns:
number of bits in the two's complement representation of this BigInteger that differ from its sign bit.

bitCnt

static int bitCnt(int val)

trailingZeroCnt

static int trailingZeroCnt(int val)

isProbablePrime

public boolean isProbablePrime(int certainty)
Suppressed: Returns true if this BigInteger is probably prime, false if it's definitely composite. If certainty is <= 0, true is returned.

Parameters:
certainty - a measure of the uncertainty that the caller is willing to tolerate: if the call returns true the probability that this BigInteger is prime exceeds (1 - 1/2certainty). The execution time of this method is proportional to the value of this parameter.
Returns:
true if this BigInteger is probably prime, false if it's definitely composite.

compareTo

public int compareTo(BigInteger val)
Suppressed: Compares this BigInteger with the specified BigInteger. This method is provided in preference to individual methods for each of the six boolean comparison operators (<, ==, >, >=, !=, <=). The suggested idiom for performing these comparisons is: (x.compareTo(y) <op> 0), where <op> is one of the six comparison operators.

Parameters:
val - BigInteger to which this BigInteger is to be compared.
Returns:
-1, 0 or 1 as this BigInteger is numerically less than, equal to, or greater than val.

compareTo

public int compareTo(Object o)
Enabled: Compares this BigInteger with the specified Object. If the Object is a BigInteger, this method behaves like compareTo(BigInteger). Otherwise, it throws a ClassCastException (as BigIntegers are comparable only to other BigIntegers).

Specified by:
compareTo in interface Comparable
Parameters:
o - Object to which this BigInteger is to be compared.
Returns:
a negative number, zero, or a positive number as this BigInteger is numerically less than, equal to, or greater than o, which must be a BigInteger.
Throws:
ClassCastException - o is not a BigInteger.
Since:
1.2
See Also:
compareTo(java.math.BigInteger), Comparable

intArrayCmp

private static int intArrayCmp(int[] arg1,
                               int[] arg2)

equals

public boolean equals(Object x)
Suppressed: Compares this BigInteger with the specified Object for equality.

Overrides:
equals in class Object
Parameters:
x - Object to which this BigInteger is to be compared.
Returns:
true if and only if the specified Object is a BigInteger whose value is numerically equal to this BigInteger.
See Also:
Object.hashCode(), java.util.Hashtable

min

public BigInteger min(BigInteger val)
Enabled: Returns the minimum of this BigInteger and val.

Parameters:
val - value with with the minimum is to be computed.
Returns:
the BigInteger whose value is the lesser of this BigInteger and val. If they are equal, either may be returned.

max

public BigInteger max(BigInteger val)
Enabled: Returns the maximum of this BigInteger and val.

Parameters:
val - value with with the maximum is to be computed.
Returns:
the BigInteger whose value is the greater of this and val. If they are equal, either may be returned.

hashCode

public int hashCode()
Suppressed: Returns the hash code for this BigInteger.

Overrides:
hashCode in class Object
Returns:
hash code for this BigInteger.
See Also:
java.lang.Object#equals(java.lang.Object), java.util.Hashtable

toString

public String toString(int radix)
Enabled: Returns the String representation of this BigInteger in the given radix. If the radix is outside the range from Character.MIN_RADIX to Character.MAX_RADIX inclusive, it will default to 10 (as is the case for Integer.toString). The digit-to-character mapping provided by Character.forDigit is used, and a minus sign is prepended if appropriate. (This representation is compatible with the (String, int) constructor.)

Parameters:
radix - radix of the String representation.
Returns:
String representation of this BigInteger in the given radix.
See Also:
Integer.toString(int, int), Character.forDigit(int, int), BigInteger(java.lang.String, int)

toString

public String toString()
Suppressed: Returns the decimal String representation of this BigInteger. The digit-to-character mapping provided by Character.forDigit is used, and a minus sign is prepended if appropriate. (This representation is compatible with the (String) constructor, and allows for String concatenation with Java's + operator.)

Overrides:
toString in class Object
Returns:
decimal String representation of this BigInteger.
See Also:
Character.forDigit(int, int), BigInteger(java.lang.String)

toByteArray

public byte[] toByteArray()
Enabled: Returns a byte array containing the two's-complement representation of this BigInteger. The byte array will be in big-endian byte-order: the most significant byte is in the zeroth element. The array will contain the minimum number of bytes required to represent this BigInteger, including at least one sign bit, which is (ceil((this.bitLength() + 1)/8)). (This representation is compatible with the (byte[]) constructor.)

Returns:
a byte array containing the two's-complement representation of this BigInteger.
See Also:
BigInteger(byte[])

intValue

public int intValue()
Enabled: Converts this BigInteger to an int. This conversion is analogous to a narrowing primitive conversion from long to int as defined in the Java Language Specification: if this BigInteger is too big to fit in an int, only the low-order 32 bits are returned. Note that this conversion can lose information about the overall magnitude of the BigInteger value as well as return a result with the opposite sign.

Specified by:
intValue in class Number
Returns:
this BigInteger converted to an int.

longValue

public long longValue()
Enabled: Converts this BigInteger to a long. This conversion is analogous to a narrowing primitive conversion from long to int as defined in the Java Language Specification: if this BigInteger is too big to fit in a long, only the low-order 64 bits are returned. Note that this conversion can lose information about the overall magnitude of the BigInteger value as well as return a result with the opposite sign.

Specified by:
longValue in class Number
Returns:
this BigInteger converted to a long.

floatValue

public float floatValue()
Enabled: Converts this BigInteger to a float. This conversion is similar to the narrowing primitive conversion from double to float defined in the Java Language Specification: if this BigInteger has too great a magnitude to represent as a float, it will be converted to Float.NEGATIVE_INFINITY or Float.POSITIVE_INFINITY as appropriate. Note that even when the return value is finite, this conversion can lose information about the precision of the BigInteger value.

Specified by:
floatValue in class Number
Returns:
this BigInteger converted to a float.

doubleValue

public double doubleValue()
Enabled: Converts this BigInteger to a double. This conversion is similar to the narrowing primitive conversion from double to float defined in the Java Language Specification: if this BigInteger has too great a magnitude to represent as a double, it will be converted to Double.NEGATIVE_INFINITY or Double.POSITIVE_INFINITY as appropriate. Note that even when the return value is finite, this conversion can lose information about the precision of the BigInteger value.

Specified by:
doubleValue in class Number
Returns:
this BigInteger converted to a double.

stripLeadingZeroInts

private static int[] stripLeadingZeroInts(int[] val)
Returns a copy of the input array stripped of any leading zero bytes.


trustedStripLeadingZeroInts

private static int[] trustedStripLeadingZeroInts(int[] val)
Returns the input array stripped of any leading zero bytes. Since the source is trusted the copying may be skipped.


stripLeadingZeroBytes

private static int[] stripLeadingZeroBytes(byte[] a)
Returns a copy of the input array stripped of any leading zero bytes.


makePositive

private static int[] makePositive(byte[] a)
Takes an array a representing a negative 2's-complement number and returns the minimal (no leading zero bytes) unsigned whose value is -a.


makePositive

private static int[] makePositive(int[] a)
Takes an array a representing a negative 2's-complement number and returns the minimal (no leading zero ints) unsigned whose value is -a.


intLength

private int intLength()
Returns the length of the two's complement representation in ints, including space for at least one sign bit.


signBit

private int signBit()

signInt

private int signInt()

getInt

private int getInt(int n)
Returns the specified int of the little-endian two's complement representation (int 0 is the least significant). The int number can be arbitrarily high (values are logically preceded by infinitely many sign ints).


firstNonzeroIntNum

private int firstNonzeroIntNum()
Returns the index of the int that contains the first nonzero int in the little-endian binary representation of the magnitude (int 0 is the least significant). If the magnitude is zero, return value is undefined.


readObject

private void readObject(ObjectInputStream s)
                 throws IOException,
                        ClassNotFoundException
Reconstitute the BigInteger instance from a stream (that is, deserialize it). The magnitude is read in as an array of bytes for historical reasons, but it is converted to an array of ints and the byte array is discarded.

IOException
ClassNotFoundException

writeObject

private void writeObject(ObjectOutputStream s)
                  throws IOException
Save the BigInteger instance to a stream. The magnitude of a BigInteger is serialized as a byte array for historical reasons.

IOException

magSerializedForm

private byte[] magSerializedForm()
Returns the mag array as an array of bytes.


compareTo

public int compareTo(Object o)
Added: Overrides the inherited 'compareTo' to coerce 'o' to a BigInteger

Specified by:
compareTo in interface Comparable
Parameters:
o - the Object to be compared.
Returns:
a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

next

public BigInteger next()
Added: The next higher integer


isNaN

public boolean isNaN()
Added: All integers are numbers, so this is always false


previous

public BigInteger previous()
Added: The next lower integer


ceil

public BigInteger ceil()
Added: Defined both here and in DoubleSugar, so you can use this to get a ceil'ed integer no matter what kind of number you start with. Similarly, the "doubleValue" message will get you a floating point value no matter what you start with.


floor

public BigInteger floor()
Added: Defined both here and in DoubleSugar, so you can use this to get a floor'ed integer no matter what kind of number you start with. Similarly, the "doubleValue" message will get you a floating point value no matter what you start with.


round

public BigInteger round()
Added: Defined both here and in DoubleSugar, so you can use this to get round'ed integer no matter what kind of number you start with. Similarly, the "doubleValue" message will get you a floating point value no matter what you start with.


butNot

public BigInteger butNot(BigInteger arg)
Added: In E, andNot() is called butNot()


mod

public BigInteger mod(BigInteger arg)
Added: Remainder from the floorDivide operation.

     (a floorDivide b)*b + (a modulo b) == a
      [ 5, 3]: ( 1* 3) +  2 ==  5
      [ 5,-3]: (-2*-3) + -1 ==  5
      [-5, 3]: (-2* 3) +  1 == -5
      [-5,-3]: ( 1*-3) + -2 == -5
 

Therefore, if the result is non-zero, the sign of the result must be the same as the sign of b, and so the result ranges from 0 inclusive to b exclusive. This corresponds to the E "%%" operator. When b >= 0, it also corresponds to Java's BigInteger.mod().


asFloat64

public double asFloat64()
Added: Returns the "best" IEEE double precision floating point equivalent to this number. If this number is representable in IEEE double precision, then that IEEE value is returned. Otherwise, convert by rounding to even.


isZero

public boolean isZero()
Added: Used for arithmetic equality


asChar

public char asChar()
Added: Returns the Unicode character with this character code.


truncate

public BigInteger truncate()
Added: Defined both here and in DoubleSugar, so you can use this to get truncate'd integer no matter what kind of number you start with. Similarly, the "doubleValue" message will get you a floating point value no matter what you start with.


approxDivide

public double approxDivide(double arg)
Added: Always gives back a Double This corresponds to the Java floating-point "/" operator and the E "/" operator.


aboveZero

public boolean aboveZero()
Added: Used in the expansion of E's ">" operator


atLeastZero

public boolean atLeastZero()
Added: Used in the expansion of E's ">=" operator


atMostZero

public boolean atMostZero()
Added: Used in the expansion of E's "<=" operator


belowZero

public boolean belowZero()
Added: Used in the expansion of E's "<" operator


complement

public BigInteger complement()
Deprecated. Use not() instead.

Added: This method supports the C-tradition expectation that unary "~" will work as bit complement.

However, the E style, supported by other set-like abstractions, is to use unary "!" instead.


floorDivide

public BigInteger floorDivide(BigInteger arg)
Added: Always gives an integer, resulting from rounding towards negative infinity, ie, flooring. This corresponds to the E "_/" operator.


truncDivide

public BigInteger truncDivide(BigInteger arg)
Added: Always gives an integer resulting from rounding towards zero, ie, truncating. This corresponds to the Java integer "/" operator. BigInteger's existing 'remainder' gives the correct remainder from the truncDivide operation.

      (a truncDivide b)*b + (a remainder b) == a
      [ 5, 3]: ( 1* 3) +  2 ==  5
      [ 5,-3]: (-1*-3) +  2 ==  5
      [-5, 3]: (-1* 3) + -2 == -5
      [-5,-3]: ( 1*-3) + -2 == -5
 

Therefore, if the result is non-zero, the sign of the result must be the same as the sign of a. This corresponds to the Java and E "%" operator.


toString64

public String toString64()
Added: Convert an integer to a base 64 string, with an optional leading minus sign.

A negative integer is encoded as a "-" followed by the encoding of the absolute magnitude. For a positive integer, the encoded length will be (4 * b.length)/3, rounded up to the next integral (non-fractional) character length.

Each base 64 digit is encoded by one of the characters


cryptoHash

public BigInteger cryptoHash()
Added: Actually, a SHA hash


fromString64

public static BigInteger fromString64(String s)
                               throws NumberFormatException
Added: Convert a base 64 string to an integer.

Parameters:
s - The string in base 64 notation, with an optional leading '-', in the format produced by BigIntegerSugar#toString64(BigInteger).
Returns:
the integer.
Throws:
NumberFormatException - if there is an invalid base 64 character in the input string.


comments?