is new.
java.lang.Objectjava.lang.Number
java.math.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 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.
| 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
(byte[] val) Translates a byte array containing the two's-complement binary representation of a BigInteger into a BigInteger. |
|
|
BigInteger
(int signum, byte[] magnitude) Translates the sign-magnitude 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's-complement 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. |
|
|
| 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 (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
(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
|
nextProbablePrime
()
Returns the first integer greater than this BigInteger that is probably prime.
|
| 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 ) . |
| static BigInteger |
probablePrime
(int bitLength,
Random
rnd) Returns a positive BigInteger that is probably prime, with the specified bitLength. |
| 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's-complement 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 |
Methods inherited from interface java.lang.
Comparable
|
|---|
compareTo
|
| Field Detail |
|---|
public static final BigInteger ZERO
public static final BigInteger ONE
TEN
public static final
BigInteger
TEN
The BigInteger constant ten.
Since:
1.5
| Constructor Detail |
|---|
public BigInteger(byte[] val)
public BigInteger(int signum,
byte[] magnitude)
public BigInteger(String val,
int radix)
public BigInteger(String val)
public BigInteger(int numBits,
Random rnd)
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.
| Method Detail |
|---|
public static BigInteger probablePrime(int bitLength,
Random rnd)
nextProbablePrime
publicstaticBigIntegernextProbablePrime
valueOf()
(long val)
Returns the first integer greater than this BigInteger that is probably prime. The probability that the number returned by this method is composite does not exceed 2
-100
. This method will never skip over a prime when searching: if it returns
p
, there is no prime
q
such that
this < q < p
.
Returns:
the first integer greater than this BigInteger that is probably prime.
Throws:
ArithmeticException
-
this < 0
.
Since:
1.5
valueOf
publicstatic BigInteger
valueOf
add(long val)
(BigIntegerval)
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.
val - value of the BigInteger to return.
a BigInteger with the specified value.
add
public BigIntegeradd
subtract(BigInteger val)
+
added to
+
subtract
public BigIntegersubtract
multiply(BigInteger val)
-
subtracted from
-
multiply
public BigIntegermultiply
divide(BigInteger val)
*
multiplied by this BigInteger.
*
divide
public BigInteger[]divide
divideAndRemainder(BigInteger val)
Returns a BigInteger whose value is
divided.
this / val
divideAndRemainder
public BigInteger[]
divideAndRemainder
remainder(BigInteger val)
Returns an array of two BigIntegers containing
(this / val)
followed by
(this % val)
.
an array of two BigIntegers: the quotient
(this / val)
is the initial element, and the remainder
(this % val)
is the final element.
remainder
public BigIntegerremainder
pow(
BigInteger
val)
(int exponent)
(this % val)
val
value by
divided, and the remainder computed.
this % val
val==0
pow
public BigIntegerpow
gcd(int exponent)
(BigIntegerval)
(this
exponent
)
Note that
exponent
is an integer rather than a BigInteger.
exponent - exponent to which this BigInteger is to be raised.
this
exponent
Throws:
ArithmeticException
-
exponent
is negative. (This would cause the operation to yield a non-integer value.)
gcd
public BigIntegergcd
abs(
BigInteger
val)
()
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.
GCD(abs(this), abs(val))
abs
public BigIntegerabs
negate()
the absolute value of this BigInteger.
abs(this)
negate
public
BigInteger
public intnegate
signum()
Returns a BigInteger whose value is
(-this)
.
-this
signum
public int
publicBigIntegersignum
mod()
(BigIntegerm)
Returns the signum function of this BigInteger.
-1, 0 or 1 as the value of this BigInteger is negative, zero or positive.
mod
public BigIntegermod
modPow(BigIntegerexponent,BigIntegerm)
(this mod m
). This method differs from
remainder
in that it always returns a
non-negative
BigInteger.
remainder(java.math.BigInteger)
modPow
public BigIntegermodPow
modInverse(BigIntegerexponent,
BigInteger m)
exponent
(Unlike
pow
, this method permits negative exponents.)
exponent - the exponent.
exponent
See Also:
modInverse(java.math.BigInteger)
modInverse
public BigIntegermodInverse
shiftLeft(
BigInteger
m)
(int n)
(this
-1
mod m)
.
m - the modulus.
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 BigIntegershiftLeft
shiftRight(int n)
<<
right
*
<<
shiftRight(int)
shiftRight
public BigIntegershiftRight
and(int n)
(BigIntegerval)
(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 / 2
n
)
.)
n - shift distance, in bits.
this >> n
See Also:
shiftLeft(int)
and
public BigIntegerand
or(BigInteger val)
&
and
are both
AND'ed
&
or
public BigIntegeror
xor(BigInteger val)
|
either
or
is
OR'ed
|
xor
public BigIntegerxor
not(
BigInteger
val)
()
(this ^ val)
BigInteger
exactly one of
this
and val are negative.)
Parameters:
val - value to be XOR'ed with this BigInteger.
this ^ val
not
public BigIntegernot
andNot()
(BigIntegerval)
(~this)
(This method returns a negative value if and only if this BigInteger is non-negative.)
~this
andNot
public
BigInteger
public booleanandNot
testBit(
BigInteger
val)
(int n)
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.)
val - value to be complemented and AND'ed with this BigInteger.
this & ~val
testBit
public boolean
publicBigIntegertestBit
setBit(int n)
Returns
true
if and only if the designated bit is set.
((this & (1<<n)) != 0)
test.
true
if and only if the designated bit is set.
setBit
public BigIntegersetBit
clearBit(int n)
set.
(this | (1<<n))
set.
this | (1<<n)
clearBit
public BigIntegerclearBit
flipBit(int n)
cleared.
(this & ~(1<<n))
clear.
this & ~(1<<n)
flipBit
public
BigInteger
public intflipBit
getLowestSetBit(int n)
()
Returns a BigInteger whose value is equivalent to this BigInteger with the designated bit flipped.
(this ^ (1<<n))
Parameters:
n - index of bit to flip.
this ^ (1<<n)
Throws:
ArithmeticException
-
n
is negative.
getLowestSetBit
public intgetLowestSetBit
bitLength()
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.
(this==0? -1 : log
(this & -this))
index of the rightmost one bit in this BigInteger.
bitLength
public intbitLength
bitCount()
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(log
2
(this < 0 ? -this : this+1)))
.)
minimal two's-complement
BigInteger,
excluding
a
bitCount
publicint
booleanbitCount
isProbablePrime()
(int certainty)
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.
number of bits in the two's complement representation of this BigInteger that differ from its sign bit.
isProbablePrime
publicboolean
intisProbablePrime
compareTo(int certainty)
(BigIntegerval)
Returns
true
if this BigInteger is probably prime,
false
if it's definitely composite. If
certainty
is
<= 0
,
true
is returned.
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.
true
if this BigInteger is probably prime,
false
if it's definitely composite.
public int compareTo(BigInteger
Objectval)
o)
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.
val
BigInteger
-1, 0
1
this BigInteger
greater than
val
.
public boolean equals(Object x)
public BigInteger min(BigInteger val)
public BigInteger max(BigInteger val)
public int hashCode()
public String toString(int radix)
public String toString()
public byte[] toByteArray()
public int intValue()
public long longValue()
public float floatValue()
public double doubleValue()