
Foundation 1.1.2  
PREV CLASS NEXT CLASS  FRAMES NO FRAMES  
SUMMARY: NESTED  FIELD  CONSTR  METHOD  DETAIL: FIELD  CONSTR  METHOD 
java.lang.Object java.lang.Number java.math.BigInteger
Immutable arbitraryprecision integers. All operations behave as if BigIntegers were represented in two'scomplement notation (like Java's primitive integer types). BigInteger provides analogues to all of Java's primitive integer operators, and all relevant methods from java.lang.Math. Additionally, BigInteger provides operations for modular arithmetic, GCD calculation, primality testing, prime generation, bit manipulation, and a few other miscellaneous operations.
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 rightshift with a negative shift distance results in a left shift, and viceversa. 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 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 nonnegative result, between 0 and (modulus  1), inclusive.
Bit operations operate on a single bit of the two'scomplement representation of their operand. If necessary, the operand is sign extended so that it contains the designated bit. None of the singlebit operations can produce a BigInteger with a different sign from 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.
For the sake of brevity and clarity, pseudocode is used throughout the descriptions of BigInteger methods. The pseudocode expression (i + j) is shorthand for "a BigInteger whose value is that of the BigInteger i plus that of the BigInteger j." The pseudocode expression (i == j) is shorthand for "true if and only if the BigInteger i represents the same value as the the BigInteger j." Other pseudocode expressions are interpreted similarly.
All methods and constructors in this class throw
NullPointerException
when passed
a null object reference for any input parameter.
BigDecimal
,
Serialized FormField Summary  
static BigInteger 
ONE
The BigInteger constant one. 
static BigInteger 
ZERO
The BigInteger constant zero. 
Constructor Summary  
BigInteger(byte[] val)
Translates a byte array containing the two'scomplement binary representation of a BigInteger into a BigInteger. 

BigInteger(int signum,
byte[] magnitude)
Translates the signmagnitude representation of a BigInteger into a BigInteger. 

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

BigInteger(int numBits,
Random rnd)
Constructs a randomly generated BigInteger, uniformly distributed over the range 0 to (2^{numBits}  1), inclusive. 

BigInteger(String val)
Translates the decimal String representation of a BigInteger into a BigInteger. 

BigInteger(String val,
int radix)
Translates the String representation of a BigInteger in the specified radix into a BigInteger. 
Method Summary  
BigInteger 
abs()
Returns a BigInteger whose value is the absolute value of this BigInteger. 
BigInteger 
add(BigInteger val)
Returns a BigInteger whose value is (this + val). 
BigInteger 
and(BigInteger val)
Returns a BigInteger whose value is (this & val). 
BigInteger 
andNot(BigInteger val)
Returns a BigInteger whose value is (this & ~val). 
int 
bitCount()
Returns the number of bits in the two's complement representation of this BigInteger that differ from its sign bit. 
int 
bitLength()
Returns the number of bits in the minimal two'scomplement representation of this BigInteger, excluding a sign bit. 
BigInteger 
clearBit(int n)
Returns a BigInteger whose value is equivalent to this BigInteger with the designated bit cleared. 
int 
compareTo(BigInteger val)
Compares this BigInteger with the specified BigInteger. 
int 
compareTo(Object o)
Compares this BigInteger with the specified Object. 
BigInteger 
divide(BigInteger val)
Returns a BigInteger whose value is (this / val). 
BigInteger[] 
divideAndRemainder(BigInteger val)
Returns an array of two BigIntegers containing (this / val) followed by (this % val). 
double 
doubleValue()
Converts this BigInteger to a double . 
boolean 
equals(Object x)
Compares this BigInteger with the specified Object for equality. 
BigInteger 
flipBit(int n)
Returns a BigInteger whose value is equivalent to this BigInteger with the designated bit flipped. 
float 
floatValue()
Converts this BigInteger to a float . 
BigInteger 
gcd(BigInteger val)
Returns a BigInteger whose value is the greatest common divisor of abs(this) and abs(val). 
int 
getLowestSetBit()
Returns the index of the rightmost (lowestorder) one bit in this BigInteger (the number of zero bits to the right of the rightmost one bit). 
int 
hashCode()
Returns the hash code for this BigInteger. 
int 
intValue()
Converts this BigInteger to an int . 
boolean 
isProbablePrime(int certainty)
Returns true if this BigInteger is probably prime, false if it's definitely composite. 
long 
longValue()
Converts this BigInteger to a long . 
BigInteger 
max(BigInteger val)
Returns the maximum of this BigInteger and val. 
BigInteger 
min(BigInteger val)
Returns the minimum of this BigInteger and val. 
BigInteger 
mod(BigInteger m)
Returns a BigInteger whose value is (this mod m). 
BigInteger 
modInverse(BigInteger m)
Returns a BigInteger whose value is (this^{1} mod m). 
BigInteger 
modPow(BigInteger exponent,
BigInteger m)
Returns a BigInteger whose value is (this^{exponent} mod m). 
BigInteger 
multiply(BigInteger val)
Returns a BigInteger whose value is (this * val). 
BigInteger 
negate()
Returns a BigInteger whose value is (this). 
BigInteger 
not()
Returns a BigInteger whose value is (~this). 
BigInteger 
or(BigInteger val)
Returns a BigInteger whose value is (this  val). 
BigInteger 
pow(int exponent)
Returns a BigInteger whose value is (this^{exponent}). 
BigInteger 
remainder(BigInteger val)
Returns a BigInteger whose value is (this % val). 
BigInteger 
setBit(int n)
Returns a BigInteger whose value is equivalent to this BigInteger with the designated bit set. 
BigInteger 
shiftLeft(int n)
Returns a BigInteger whose value is (this << n). 
BigInteger 
shiftRight(int n)
Returns a BigInteger whose value is (this >> n). 
int 
signum()
Returns the signum function of this BigInteger. 
BigInteger 
subtract(BigInteger val)
Returns a BigInteger whose value is (this  val). 
boolean 
testBit(int n)
Returns true if and only if the designated bit is set. 
byte[] 
toByteArray()
Returns a byte array containing the two'scomplement representation of this BigInteger. 
String 
toString()
Returns the decimal String representation of this BigInteger. 
String 
toString(int radix)
Returns the String representation of this BigInteger in the given radix. 
static BigInteger 
valueOf(long val)
Returns a BigInteger whose value is equal to that of the specified long . 
BigInteger 
xor(BigInteger val)
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 
public static final BigInteger ZERO
public static final BigInteger ONE
Constructor Detail 
public BigInteger(byte[] val)
val
 bigendian two'scomplement binary representation of
BigInteger.
NumberFormatException
 val is zero bytes long.public BigInteger(int signum, byte[] magnitude)
signum
 signum of the number (1 for negative, 0 for zero, 1
for positive).magnitude
 bigendian binary representation of the magnitude of
the number.
NumberFormatException
 signum is not one of the three
legal values (1, 0, and 1), or signum is 0 and
magnitude contains one or more nonzero bytes.public BigInteger(String val, int radix)
val
 String representation of BigInteger.radix
 radix to be used in interpreting val.
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.Character.digit(char, int)
public BigInteger(String val)
val
 decimal String representation of BigInteger.
NumberFormatException
 val is not a valid representation
of a BigInteger.Character.digit(char, int)
public BigInteger(int numBits, Random rnd)
numBits
 maximum bitLength of the new BigInteger.rnd
 source of randomness to be used in computing the new
BigInteger.
IllegalArgumentException
 numBits is negative.bitLength()
public BigInteger(int bitLength, int certainty, Random rnd)
It is recommended that the probablePrime
method be used in preference to this constructor unless there
is a compelling need to specify a certainty.
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/2^{certainty}). 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.
ArithmeticException
 bitLength < 2.bitLength()
Method Detail 
public static BigInteger valueOf(long val)
long
. This "static factory method" is
provided in preference to a (long
) constructor
because it allows for reuse of frequently used BigIntegers.
val
 value of the BigInteger to return.
public BigInteger add(BigInteger val)
val
 value to be added to this BigInteger.
public BigInteger subtract(BigInteger val)
val
 value to be subtracted from this BigInteger.
public BigInteger multiply(BigInteger val)
val
 value to be multiplied by this BigInteger.
public BigInteger divide(BigInteger val)
val
 value by which this BigInteger is to be divided.
ArithmeticException
 val==0public BigInteger[] divideAndRemainder(BigInteger val)
val
 value by which this BigInteger is to be divided, and the
remainder computed.
ArithmeticException
 val==0public BigInteger remainder(BigInteger val)
val
 value by which this BigInteger is to be divided, and the
remainder computed.
ArithmeticException
 val==0public BigInteger pow(int exponent)
exponent
 exponent to which this BigInteger is to be raised.
ArithmeticException
 exponent is negative. (This would
cause the operation to yield a noninteger value.)public BigInteger gcd(BigInteger val)
val
 value with with the GCD is to be computed.
public BigInteger abs()
public BigInteger negate()
public int signum()
public BigInteger mod(BigInteger m)
m
 the modulus.
ArithmeticException
 m <= 0remainder(java.math.BigInteger)
public BigInteger modPow(BigInteger exponent, BigInteger m)
exponent
 the exponent.m
 the modulus.
ArithmeticException
 m <= 0modInverse(java.math.BigInteger)
public BigInteger modInverse(BigInteger m)
m
 the modulus.
ArithmeticException
 m <= 0, or this BigInteger
has no multiplicative inverse mod m (that is, this BigInteger
is not relatively prime to m).public BigInteger shiftLeft(int n)
n
 shift distance, in bits.
shiftRight(int)
public BigInteger shiftRight(int n)
n
 shift distance, in bits.
shiftLeft(int)
public BigInteger and(BigInteger val)
val
 value to be AND'ed with this BigInteger.
public BigInteger or(BigInteger val)
val
 value to be OR'ed with this BigInteger.
public BigInteger xor(BigInteger val)
val
 value to be XOR'ed with this BigInteger.
public BigInteger not()
public BigInteger andNot(BigInteger val)
val
 value to be complemented and AND'ed with this BigInteger.
public boolean testBit(int n)
n
 index of bit to test.
ArithmeticException
 n is negative.public BigInteger setBit(int n)
n
 index of bit to set.
ArithmeticException
 n is negative.public BigInteger clearBit(int n)
n
 index of bit to clear.
ArithmeticException
 n is negative.public BigInteger flipBit(int n)
n
 index of bit to flip.
ArithmeticException
 n is negative.public int getLowestSetBit()
public int bitLength()
public int bitCount()
public boolean isProbablePrime(int certainty)
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/2^{certainty}). The execution time of
this method is proportional to the value of this parameter.
public int compareTo(BigInteger val)
val
 BigInteger to which this BigInteger is to be compared.
public int compareTo(Object o)
compareTo
in interface Comparable
o
 Object to which this BigInteger is to be compared.
ClassCastException
 o is not a BigInteger.compareTo(java.math.BigInteger)
,
Comparable
public boolean equals(Object x)
equals
in class Object
x
 Object to which this BigInteger is to be compared.
Object.hashCode()
,
Hashtable
public BigInteger min(BigInteger val)
val
 value with with the minimum is to be computed.
public BigInteger max(BigInteger val)
val
 value with with the maximum is to be computed.
public int hashCode()
hashCode
in class Object
Object.equals(java.lang.Object)
,
Hashtable
public String toString(int radix)
Character.MIN_RADIX
to Character.MAX_RADIX
inclusive,
it will default to 10 (as is the case for
Integer.toString). The digittocharacter mapping
provided by Character.forDigit is used, and a minus
sign is prepended if appropriate. (This representation is
compatible with the (String,
int
)
constructor.)
radix
 radix of the String representation.
Integer.toString(int, int)
,
Character.forDigit(int, int)
,
BigInteger(java.lang.String, int)
public String toString()
(String)
constructor, and
allows for String concatenation with Java's + operator.)
toString
in class Object
Character.forDigit(int, int)
,
BigInteger(java.lang.String)
public byte[] toByteArray()
(byte[])
constructor.)
BigInteger(byte[])
public int intValue()
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 loworder 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.
intValue
in class Number
int
.public long longValue()
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 loworder 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.
longValue
in class Number
long
.public float floatValue()
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.
floatValue
in class Number
float
.public double doubleValue()
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.
doubleValue
in class Number
double
.

Foundation 1.1.2  
PREV CLASS NEXT CLASS  FRAMES NO FRAMES  
SUMMARY: NESTED  FIELD  CONSTR  METHOD  DETAIL: FIELD  CONSTR  METHOD 