All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class java.math.BigInteger

java.lang.Object
   |
   +----java.lang.Number
           |
           +----java.math.BigInteger

public class BigInteger
extends Number
Immutable arbitrary-precision integers. All operations behave as if BigIntegers were represented in two's-complement notation (like Java's primitive integer types). BigIntegers provide analogues to all of Java's primitive integer operators, and all relevant static methods from java.lang.Math. Additionally, BigIntegers provide operations for modular arithmetic, GCD calculation, primality testing, prime generation, single-bit manipulation, and a few other odds and ends.

Semantics of arithmetic operations exactly mimic those of java's integer arithmetic operators, as defined in The Java Language Specification. For example, division by zero throws an ArithmeticException, and division of a negative by a positive yields a negative (or zero) remainder. All of the details in the Spec concerning overflow are ignored, as BigIntegers are made as large as necessary to accommodate the results of an operation.

Semantics of shift operations extend those of Java's shift operators to allow for negative shift distances. A right-shift with a negative shift distance results in a left shift, and vice-versa. The unsigned right shift operator (>>>) is omitted, as this operation makes little sense in combination with the "infinite word size" abstraction provided by this class.

Semantics of bitwise logical operations are are exactly mimic those of Java's bitwise integer operators. The Binary operators (and, or, xor) implicitly perform sign extension on the shorter of the two operands prior to performing the operation.

Comparison operations perform signed integer comparisons, analogous to those performed by java's relational and equality operators.

Modular arithmetic operations are provided to compute residues, perform exponentiation, and compute multiplicative inverses. These methods always return a non-negative result, between 0 and (modulus - 1), inclusive.

Single-bit operations operate on a single bit of the two's-complement representation of their operand. If necessary, the operand is sign extended so that it contains the designated bit. None of the single-bit operations can produce a number with a different sign from the the BigInteger being operated on, as they affect only a single bit, and the "infinite word size" abstraction provided by this class ensures that there are infinitely many "virtual sign bits" preceding each BigInteger.

See Also:
BigDecimal

Constructor Index

 o BigInteger(byte[])
Translates a byte array containing the two's-complement representation of a (signed) integer into a BigInteger.
 o BigInteger(int, byte[])
Translates the sign-magnitude representation of an integer into a BigInteger.
 o BigInteger(int, int, Random)
Returns a randomly selected BigInteger with the specified bitLength that is probably prime.
 o BigInteger(int, Random)
Returns a random number uniformly distributed on [0, 2**numBits - 1] (assuming a fair source of random bits is provided in rndSrc).
 o BigInteger(String)
Translates a string containing an optional minus sign followed by a sequence of one or more decimal digits into a BigInteger.
 o BigInteger(String, int)
Translates a string containing an optional minus sign followed by a sequence of one or more digits in the specified radix into a BigInteger.

Method Index

 o abs()
Returns a BigInteger whose value is the absolute value of this number.
 o add(BigInteger)
Returns a BigInteger whose value is (this + val).
 o and(BigInteger)
Returns a BigInteger whose value is (this & val).
 o andNot(BigInteger)
Returns a BigInteger whose value is (this & ~val).
 o bitCount()
Returns the number of bits in the two's complement representation of this number that differ from its sign bit.
 o bitLength()
Returns the number of bits in the minimal two's-complement representation of this number, *excluding* a sign bit, i.e., (ceil(log2(this < 0 ? -this : this + 1))).
 o clearBit(int)
Returns a BigInteger whose value is equivalent to this number with the designated bit cleared.
 o compareTo(BigInteger)
Returns -1, 0 or 1 as this number is less than, equal to, or greater than val.
 o divide(BigInteger)
Returns a BigInteger whose value is (this / val).
 o divideAndRemainder(BigInteger)
Returns an array of two BigIntegers.
 o doubleValue()
Converts the number to a double.
 o equals(Object)
Returns true iff x is a BigInteger whose value is equal to this number.
 o flipBit(int)
Returns a BigInteger whose value is equivalent to this number with the designated bit flipped.
 o floatValue()
Converts this number to a float.
 o gcd(BigInteger)
Returns a BigInteger whose value is the greatest common denominator of abs(this) and abs(val).
 o getLowestSetBit()
Returns the index of the rightmost (lowest-order) one bit in this number (i.e., the number of zero bits to the right of the rightmost one bit).
 o hashCode()
Computes a hash code for this object.
 o intValue()
Converts this number to an int.
 o isProbablePrime(int)
Returns true if this BigInteger is probably prime, false if it's definitely composite.
 o longValue()
Converts this number to a long.
 o max(BigInteger)
Returns the BigInteger whose value is the greater of this and val.
 o min(BigInteger)
Returns the BigInteger whose value is the lesser of this and val.
 o mod(BigInteger)
Returns a BigInteger whose value is this mod m.
 o modInverse(BigInteger)
Returns modular multiplicative inverse of this, mod m.
 o modPow(BigInteger, BigInteger)
Returns a BigInteger whose value is (this ** exponent) mod m.
 o multiply(BigInteger)
Returns a BigInteger whose value is (this * val).
 o negate()
Returns a BigInteger whose value is (-1 * this).
 o not()
Returns a BigInteger whose value is (~this).
 o or(BigInteger)
Returns a BigInteger whose value is (this | val).
 o pow(int)
Returns a BigInteger whose value is (this ** exponent).
 o remainder(BigInteger)
Returns a BigInteger whose value is (this % val).
 o setBit(int)
Returns a BigInteger whose value is equivalent to this number with the designated bit set.
 o shiftLeft(int)
Returns a BigInteger whose value is (this << n).
 o shiftRight(int)
Returns a BigInteger whose value is (this >> n).
 o signum()
Returns the signum function of this number (i.e., -1, 0 or 1 as the value of this number is negative, zero or positive).
 o subtract(BigInteger)
Returns a BigInteger whose value is (this - val).
 o testBit(int)
Returns true iff the designated bit is set.
 o toByteArray()
Returns the two's-complement representation of this number.
 o toString()
Returns the string representation of this number, radix 10.
 o toString(int)
Returns the string representation of this number in the given radix.
 o valueOf(long)
Returns a BigInteger with the specified value.
 o xor(BigInteger)
Returns a BigInteger whose value is (this ^ val).

Constructors

 o BigInteger
    method BigInteger(val=byte[]) public Signals NumberFormatException
Translates a byte array containing the two's-complement representation of a (signed) integer into a BigInteger. The input array is assumed to be big-endian (i.e., the most significant byte is in the [0] position). (The most significant bit of the most significant byte is the sign bit.) The array must contain at least one byte or a NumberFormatException will be thrown.

 o BigInteger
    method BigInteger(signum=int, -
                      magnitude=byte[]) public Signals NumberFormatException
Translates the sign-magnitude representation of an integer into a BigInteger. The sign is represented as an integer signum value (-1 for negative, 0 for zero, 1 for positive). The magnitude is represented as a big-endian byte array (i.e., the most significant byte is in the [0] position). An invalid signum value or a 0 signum value coupled with a nonzero magnitude will result in a NumberFormatException. A zero length magnitude array is permissible, and will result in in a value of 0 (irrespective of the given signum value).

 o BigInteger
    method BigInteger(val=String, -
                      radix=int) public Signals NumberFormatException
Translates a string containing an optional minus sign followed by a sequence of one or more digits in the specified radix into a BigInteger. The character-to-digit mapping is provided by Character.digit. Any extraneous characters (including whitespace), or a radix outside the range from Character.MIN_RADIX(2) to Character.MAX_RADIX(36), inclusive, will result in a NumberFormatException.

 o BigInteger
    method BigInteger(val=String) public Signals NumberFormatException
Translates a string containing an optional minus sign followed by a sequence of one or more decimal digits into a BigInteger. The character-to-digit mapping is provided by Character.digit. Any extraneous characters (including whitespace) will result in a NumberFormatException.

 o BigInteger
    method BigInteger(numBits=int, -
                      rndSrc=Random) public Signals IllegalArgumentException
Returns a random number uniformly distributed on [0, 2**numBits - 1] (assuming a fair source of random bits is provided in rndSrc). Note that this constructor always returns a non-negative BigInteger. Throws an IllegalArgumentException if numBits < 0.

 o BigInteger
    method BigInteger(bitLength=int, -
                      certainty=int, -
                      rnd=Random) public 
Returns a randomly selected BigInteger with the specified bitLength that is probably prime. The certainty parameter is a measure of the uncertainty that the caller is willing to tolerate: the probability that the number is prime will exceed 1 - 1/2**certainty. The execution time is proportional to the value of the certainty parameter. The given random number generator is used to select candidates to be tested for primality. Throws an ArithmeticException if bitLength < 2.

Methods

 o valueOf
    method valueOf(val=long) returns BigInteger public  static   
Returns a BigInteger with the specified value. This factory is provided in preference to a (long) constructor because it allows for reuse of frequently used BigIntegers (like 0 and 1), obviating the need for exported constants.

 o add
    method add(val=BigInteger) returns BigInteger public     Signals ArithmeticException
Returns a BigInteger whose value is (this + val).

 o subtract
    method subtract(val=BigInteger) returns BigInteger public     
Returns a BigInteger whose value is (this - val).

 o multiply
    method multiply(val=BigInteger) returns BigInteger public     
Returns a BigInteger whose value is (this * val).

 o divide
    method divide(val=BigInteger) returns BigInteger public     Signals ArithmeticException
Returns a BigInteger whose value is (this / val). Throws an ArithmeticException if val == 0.

 o remainder
    method remainder(val=BigInteger) returns BigInteger public     Signals ArithmeticException
Returns a BigInteger whose value is (this % val). Throws an ArithmeticException if val == 0.

 o divideAndRemainder
    method divideAndRemainder(val=BigInteger) returns BigInteger[] public     Signals ArithmeticException
Returns an array of two BigIntegers. The first ([0]) element of the return value is the quotient (this / val), and the second ([1]) element is the remainder (this % val). Throws an ArithmeticException if val == 0.

 o pow
    method pow(exponent=int) returns BigInteger public     Signals ArithmeticException
Returns a BigInteger whose value is (this ** exponent). Throws an ArithmeticException if exponent < 0 (as the operation would yield a non-integer value). Note that exponent is an integer rather than a BigInteger.

 o gcd
    method gcd(val=BigInteger) returns BigInteger public     
Returns a BigInteger whose value is the greatest common denominator of abs(this) and abs(val). Returns 0 if this == 0 && val == 0.

 o abs
    method abs() returns BigInteger public     
Returns a BigInteger whose value is the absolute value of this number.

 o negate
    method negate() returns BigInteger public     
Returns a BigInteger whose value is (-1 * this).

 o signum
    method signum() returns int public     
Returns the signum function of this number (i.e., -1, 0 or 1 as the value of this number is negative, zero or positive).

 o mod
    method mod(m=BigInteger) returns BigInteger public     
Returns a BigInteger whose value is this mod m. Throws an ArithmeticException if m <= 0.

 o modPow
    method modPow(exponent=BigInteger, -
                  m=BigInteger) returns BigInteger public     
Returns a BigInteger whose value is (this ** exponent) mod m. (If exponent == 1, the returned value is (this mod m). If exponent < 0, the returned value is the modular multiplicative inverse of (this ** -exponent).) Throws an ArithmeticException if m <= 0.

 o modInverse
    method modInverse(m=BigInteger) returns BigInteger public     Signals ArithmeticException
Returns modular multiplicative inverse of this, mod m. Throws an ArithmeticException if m <= 0 or this has no multiplicative inverse mod m (i.e., gcd(this, m) != 1).

 o shiftLeft
    method shiftLeft(n=int) returns BigInteger public     
Returns a BigInteger whose value is (this << n). (Computes floor(this * 2**n).)

 o shiftRight
    method shiftRight(n=int) returns BigInteger public     
Returns a BigInteger whose value is (this >> n). Sign extension is performed. (Computes floor(this / 2**n).)

 o and
    method and(val=BigInteger) returns BigInteger public     
Returns a BigInteger whose value is (this & val). (This method returns a negative number iff this and val are both negative.)

 o or
    method or(val=BigInteger) returns BigInteger public     
Returns a BigInteger whose value is (this | val). (This method returns a negative number iff either this or val is negative.)

 o xor
    method xor(val=BigInteger) returns BigInteger public     
Returns a BigInteger whose value is (this ^ val). (This method returns a negative number iff exactly one of this and val are negative.)

 o not
    method not() returns BigInteger public     
Returns a BigInteger whose value is (~this). (This method returns a negative value iff this number is non-negative.)

 o andNot
    method andNot(val=BigInteger) returns BigInteger public     
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 number iff this is negative and val is positive.)

 o testBit
    method testBit(n=int) returns boolean public     Signals ArithmeticException
Returns true iff the designated bit is set. (Computes ((this & (1<
 o setBit
    method setBit(n=int) returns BigInteger public     Signals ArithmeticException
Returns a BigInteger whose value is equivalent to this number with the designated bit set. (Computes (this | (1<
 o clearBit
    method clearBit(n=int) returns BigInteger public     Signals ArithmeticException
Returns a BigInteger whose value is equivalent to this number with the designated bit cleared. (Computes (this & ~(1<
 o flipBit
    method flipBit(n=int) returns BigInteger public     Signals ArithmeticException
Returns a BigInteger whose value is equivalent to this number with the designated bit flipped. (Computes (this ^ (1<
 o getLowestSetBit
    method getLowestSetBit() returns int public     
Returns the index of the rightmost (lowest-order) one bit in this number (i.e., the number of zero bits to the right of the rightmost one bit). Returns -1 if this number contains no one bits. (Computes (this==0? -1 : log2(this & -this)).)

 o bitLength
    method bitLength() returns int public     
Returns the number of bits in the minimal two's-complement representation of this number, *excluding* a sign bit, i.e., (ceil(log2(this < 0 ? -this : this + 1))). (For positive numbers, this is equivalent to the number of bits in the ordinary binary representation.)

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

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

 o compareTo
    method compareTo(val=BigInteger) returns int public     
Returns -1, 0 or 1 as this number is less than, equal to, or greater than val. 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) 0), where is one of the six comparison operators.

 o equals
    method equals(x=Object) returns boolean public     
Returns true iff x is a BigInteger whose value is equal to this number. This method is provided so that BigIntegers can be used as hash keys.

Overrides:
equals in class Object
 o min
    method min(val=BigInteger) returns BigInteger public     
Returns the BigInteger whose value is the lesser of this and val. If the values are equal, either may be returned.

 o max
    method max(val=BigInteger) returns BigInteger public     
Returns the BigInteger whose value is the greater of this and val. If the values are equal, either may be returned.

 o hashCode
    method hashCode() returns int public     
Computes a hash code for this object.

Overrides:
hashCode in class Object
 o toString
    method toString(radix=int) returns String public     
Returns the string representation of this number in the given radix. If the radix is outside the range from Character.MIN_RADIX(2) to Character.MAX_RADIX(36) 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.)

 o toString
    method toString() returns String public     
Returns the string representation of this number, radix 10. 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
 o toByteArray
    method toByteArray() returns byte[] public     
Returns the two's-complement representation of this number. The array is big-endian (i.e., the most significant byte is in the [0] position). The array contains the minimum number of bytes required to represent the number (ceil((this.bitLength() + 1)/8)). (This representation is compatible with the (byte[]) constructor.)

 o intValue
    method intValue() returns int public     
Converts this number to an int. Standard narrowing primitive conversion as per The Java Language Specification.

Overrides:
intValue in class Number
 o longValue
    method longValue() returns long public     
Converts this number to a long. Standard narrowing primitive conversion as per The Java Language Specification.

Overrides:
longValue in class Number
 o floatValue
    method floatValue() returns float public     
Converts this number to a float. Similar to the double-to-float narrowing primitive conversion defined in The Java Language Specification: if the number has too great a magnitude to represent as a float, it will be converted to infinity or negative infinity, as appropriate.

Overrides:
floatValue in class Number
 o doubleValue
    method doubleValue() returns double public     
Converts the number to a double. Similar to the double-to-float narrowing primitive conversion defined in The Java Language Specification: if the number has too great a magnitude to represent as a double, it will be converted to infinity or negative infinity, as appropriate.

Overrides:
doubleValue in class Number

All Packages  Class Hierarchy  This Package  Previous  Next  Index