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 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.

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 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, pseudo-code is used throughout the
descriptions of BigInteger methods. The pseudo-code expression
(i + j) is shorthand for "a BigInteger whose value is
that of the BigInteger i plus that of the BigInteger j."
The pseudo-code expression (i == j) is shorthand for
"true if and only if the BigInteger i represents the same
value as the the BigInteger j." Other pseudo-code expressions are
interpreted similarly.

ZERO

ONE

BigInteger

public BigInteger(byte[] val)

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.

BigInteger

public BigInteger(int signum,
byte[] magnitude)

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

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 (2) to
Character.MAX_RADIX (36), inclusive.

BigInteger

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).

BigInteger

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.

BigInteger

Constructs a randomly generated positive BigInteger that is probably
prime, with the specified bitLength.

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.

valueOf

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.

andNot

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)

Returns true if and only if the designated bit is set.
(Computes ((this & (1<<n)) != 0).)

getLowestSetBit

public int getLowestSetBit()

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()

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.

bitCount

public int bitCount()

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.

isProbablePrime

public boolean isProbablePrime(int certainty)

Returns true if this BigInteger is probably prime,
false if it's definitely composite.

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

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

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).

hashCode

toString

Returns the String representation of this BigInteger 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.)

toString

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.)

toByteArray

public byte[] toByteArray()

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.

intValue

public int intValue()

Converts this BigInteger to an int. Standard narrowing primitive
conversion 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.

longValue

public long longValue()

Converts this BigInteger to a long. Standard narrowing primitive
conversion 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.

floatValue

public float floatValue()

Converts this BigInteger to a float. Similar to the double-to-float
narrowing primitive conversion defined in The Java Language
Specification: if this BigInteger has too great a magnitude to
represent as a float, it will be converted to infinity or negative
infinity, as appropriate.

doubleValue

public double doubleValue()

Converts this BigInteger to a double. Similar to the double-to-float
narrowing primitive conversion defined in The Java Language
Specification: if this BigInteger has too great a magnitude to
represent as a double, it will be converted to infinity or negative
infinity, as appropriate.