|
||||||||||
| 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
, Comparable
<BigInteger
>public class BigInteger

<BigInteger
>Immutable arbitrary-precision integers. All operations behave as if BigIntegers were represented in two's-complement 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 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.
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.
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 BigInteger j." Other pseudo-code 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 Form| Field Summary | |
|---|---|
static BigInteger |
ONE
The BigInteger constant one. |
static BigInteger |
TEN
The BigInteger constant ten. |
static BigInteger |
ZERO
The BigInteger constant zero. |
| Constructor Summary | |
|---|---|
BigInteger
Translates a byte array containing the two's-complement binary representation of a BigInteger into a BigInteger. |
|
BigInteger
Translates the sign-magnitude representation of a BigInteger into a BigInteger. |
|
BigInteger
Constructs a randomly generated positive BigInteger that is probably prime, with the specified bitLength. |
|
BigInteger
Constructs a randomly generated BigInteger, uniformly distributed over the range 0 to (2numBits - 1), inclusive. |
|
BigInteger
Translates the decimal String representation of a BigInteger into a BigInteger. |
|
BigInteger
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
Returns a BigInteger whose value is (this + val). |
BigInteger |
and
Returns a BigInteger whose value is (this & val). |
BigInteger |
andNot
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's-complement representation of this BigInteger, excluding a sign bit. |
BigInteger |
clearBit
Returns a BigInteger whose value is equivalent to this BigInteger with the designated bit cleared. |
int |
compareTo
Compares this BigInteger with the specified BigInteger. |
BigInteger |
divide
Returns a BigInteger whose value is (this / val). |
BigInteger |
divideAndRemainder
Returns an array of two BigIntegers containing (this / val) followed by (this % val). |
double |
doubleValue
Converts this BigInteger to a double. |
boolean |
equals
Compares this BigInteger with the specified Object for equality. |
BigInteger |
flipBit
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
Returns a BigInteger whose value is the greatest common divisor of abs(this) and abs(val). |
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). |
int |
hashCode
Returns the hash code for this BigInteger. |
int |
intValue
Converts this BigInteger to an int. |
boolean |
isProbablePrime
Returns true if this BigInteger is probably prime, false if it's definitely composite. |
long |
longValue
Converts this BigInteger to a long. |
BigInteger |
max
Returns the maximum of this BigInteger and val. |
BigInteger |
min
Returns the minimum of this BigInteger and val. |
BigInteger |
mod
Returns a BigInteger whose value is (this mod m). |
BigInteger |
modInverse
Returns a BigInteger whose value is (this-1 mod m). |
BigInteger |
modPow
Returns a BigInteger whose value is (thisexponent mod m). |
BigInteger |
multiply
Returns a BigInteger whose value is (this * val). |
BigInteger |
negate
Returns a BigInteger whose value is (-this). |
BigInteger |
nextProbablePrime
Returns the first integer greater than this BigInteger that
is probably prime. |
BigInteger |
not
Returns a BigInteger whose value is (~this). |
BigInteger |
or
Returns a BigInteger whose value is (this | val). |
BigInteger |
pow
Returns a BigInteger whose value is (thisexponent). |
static BigInteger |
probablePrime
Returns a positive BigInteger that is probably prime, with the specified bitLength. |
BigInteger |
remainder
Returns a BigInteger whose value is (this % val). |
BigInteger |
setBit
Returns a BigInteger whose value is equivalent to this BigInteger with the designated bit set. |
BigInteger |
shiftLeft
Returns a BigInteger whose value is (this << n). |
BigInteger |
shiftRight
Returns a BigInteger whose value is (this >> n). |
int |
signum
Returns the signum function of this BigInteger. |
BigInteger |
subtract
Returns a BigInteger whose value is (this - val). |
boolean |
testBit
Returns true if and only if the designated bit is set. |
byte[] |
toByteArray
Returns a byte array containing the two's-complement representation of this BigInteger. |
String |
toString
Returns the decimal String representation of this BigInteger. |
String |
toString
Returns the String representation of this BigInteger in the given radix. |
static BigInteger |
valueOf
Returns a BigInteger whose value is equal to that of the specified long. |
BigInteger |
xor
Returns a BigInteger whose value is (this ^ val). |
Methods inherited from class java.lang.Number ![]() |
|---|
byteValue |
Methods inherited from class java.lang.Object ![]() |
|---|
clone |
| Field Detail |
|---|

public static final BigInteger![]()
![]()
ZERO

public static final BigInteger![]()
![]()
ONE

public static final BigInteger![]()
![]()
TEN
| Constructor Detail |
|---|

public BigInteger(byte[] val)
val - big-endian two's-complement 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 - big-endian 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 non-zero 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/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.
ArithmeticException

- bitLength < 2.bitLength

| Method Detail |
|---|

public static BigInteger![]()
![]()
probablePrime(int bitLength, Random
![]()
![]()
rnd)
bitLength - bitLength of the returned BigInteger.rnd - source of random bits used to select candidates to be
tested for primality.
ArithmeticException

- bitLength < 2.bitLength