public
class
BigInteger
extends Number
implements
Comparable<BigInteger>,
Serializable
java.lang.Object | ||
↳ | java.lang.Number | |
↳ | java.math.BigInteger |
An immutable arbitrary-precision signed integer.
BitSet
for high-performance bitwise operations on
arbitrarily-large sequences of bits.
Fields | |
---|---|
public
static
final
BigInteger |
ONE
The |
public
static
final
BigInteger |
TEN
The |
public
static
final
BigInteger |
ZERO
The |
Public constructors | |
---|---|
BigInteger(int numBits, Random random)
Constructs a random non-negative |
|
BigInteger(int bitLength, int certainty, Random random)
Constructs a random |
|
BigInteger(String value)
Constructs a new |
|
BigInteger(String value, int radix)
Constructs a new |
|
BigInteger(int signum, byte[] magnitude)
Constructs a new |
|
BigInteger(byte[] value)
Constructs a new |
Public methods | |
---|---|
BigInteger
|
abs()
Returns a |
BigInteger
|
add(BigInteger value)
Returns a |
BigInteger
|
and(BigInteger value)
Returns a |
BigInteger
|
andNot(BigInteger value)
Returns a |
int
|
bitCount()
Returns the number of bits in the two's complement representation of
|
int
|
bitLength()
Returns the length of the value's two's complement representation without leading zeros for positive numbers / without leading ones for negative values. |
BigInteger
|
clearBit(int n)
Returns a |
int
|
compareTo(BigInteger value)
Compares this |
BigInteger
|
divide(BigInteger divisor)
Returns a |
BigInteger[]
|
divideAndRemainder(BigInteger divisor)
Returns a two element |
double
|
doubleValue()
Returns this |
boolean
|
equals(Object x)
Indicates whether some other object is "equal to" this one. |
BigInteger
|
flipBit(int n)
Returns a |
float
|
floatValue()
Returns this |
BigInteger
|
gcd(BigInteger value)
Returns a |
int
|
getLowestSetBit()
Returns the position of the lowest set bit in the two's complement
representation of this |
int
|
hashCode()
Returns a hash code value for the object. |
int
|
intValue()
Returns this |
boolean
|
isProbablePrime(int certainty)
Tests whether this |
long
|
longValue()
Returns this |
BigInteger
|
max(BigInteger value)
Returns the maximum of this |
BigInteger
|
min(BigInteger value)
Returns the minimum of this |
BigInteger
|
mod(BigInteger m)
Returns a |
BigInteger
|
modInverse(BigInteger m)
Returns a |
BigInteger
|
modPow(BigInteger exponent, BigInteger modulus)
Returns a |
BigInteger
|
multiply(BigInteger value)
Returns a |
BigInteger
|
negate()
Returns a |
BigInteger
|
nextProbablePrime()
Returns the smallest integer x > |
BigInteger
|
not()
Returns a |
BigInteger
|
or(BigInteger value)
Returns a |
BigInteger
|
pow(int exp)
Returns a |
static
BigInteger
|
probablePrime(int bitLength, Random random)
Returns a random positive |
BigInteger
|
remainder(BigInteger divisor)
Returns a |
BigInteger
|
setBit(int n)
Returns a |
BigInteger
|
shiftLeft(int n)
Returns a |
BigInteger
|
shiftRight(int n)
Returns a |
int
|
signum()
Returns the sign of this |
BigInteger
|
subtract(BigInteger value)
Returns a |
boolean
|
testBit(int n)
Tests whether the bit at position n in |
byte[]
|
toByteArray()
Returns the two's complement representation of this |
String
|
toString()
Returns a string representation of this |
String
|
toString(int radix)
Returns a string containing a string representation of this |
static
BigInteger
|
valueOf(long value)
Returns a |
BigInteger
|
xor(BigInteger value)
Returns a |
Inherited methods | |
---|---|
From
class
java.lang.Number
| |
From
class
java.lang.Object
| |
From
interface
java.lang.Comparable
|
BigInteger (int numBits, Random random)
Constructs a random non-negative BigInteger
instance in the range
[0, pow(2, numBits)-1]
.
Parameters | |
---|---|
numBits |
int :
maximum length of the new BigInteger in bits. |
random |
Random :
is the random number generator to be used. |
Throws | |
---|---|
IllegalArgumentException |
if numBits < 0.
|
BigInteger (int bitLength, int certainty, Random random)
Constructs a random BigInteger
instance in the range [0,
pow(2, bitLength)-1]
which is probably prime. The probability that the
returned BigInteger
is prime is greater than
1 - 1/2<sup>certainty</sup>)
.
Note: the Random
argument is ignored if
bitLength >= 16
, where this implementation will use OpenSSL's
BN_generate_prime_ex
as a source of cryptographically strong pseudo-random numbers.
Parameters | |
---|---|
bitLength |
int :
length of the new BigInteger in bits. |
certainty |
int :
tolerated primality uncertainty. |
random |
Random
|
Throws | |
---|---|
ArithmeticException |
if bitLength < 2 . |
BigInteger (String value)
Constructs a new BigInteger
by parsing value
. The string
representation consists of an optional plus or minus sign followed by a
non-empty sequence of decimal digits. Digits are interpreted as if by
Character.digit(char,10)
.
Parameters | |
---|---|
value |
String :
string representation of the new BigInteger . |
Throws | |
---|---|
NullPointerException |
if value == null . |
NumberFormatException |
if value is not a valid
representation of a BigInteger .
|
BigInteger (String value, int radix)
Constructs a new BigInteger
instance by parsing value
.
The string representation consists of an optional plus or minus sign
followed by a non-empty sequence of digits in the specified radix. Digits
are interpreted as if by Character.digit(char, radix)
.
Parameters | |
---|---|
value |
String :
string representation of the new BigInteger . |
radix |
int :
the base to be used for the conversion. |
Throws | |
---|---|
NullPointerException |
if value == null . |
NumberFormatException |
if value is not a valid
representation of a BigInteger or if radix <
Character.MIN_RADIX or radix > Character.MAX_RADIX .
|
BigInteger (int signum, byte[] magnitude)
Constructs a new BigInteger
instance with the given sign and
magnitude.
Parameters | |
---|---|
signum |
int :
sign of the new BigInteger (-1 for negative, 0 for
zero, 1 for positive). |
magnitude |
byte :
magnitude of the new BigInteger with the most
significant byte first. |
Throws | |
---|---|
NullPointerException |
if magnitude == null . |
NumberFormatException |
if the sign is not one of -1, 0, 1 or if the sign is zero and the magnitude contains non-zero entries. |
BigInteger (byte[] value)
Constructs a new BigInteger
from the given two's complement
representation. The most significant byte is the entry at index 0. The
most significant bit of this entry determines the sign of the new BigInteger
instance. The array must be nonempty.
Parameters | |
---|---|
value |
byte :
two's complement representation of the new BigInteger . |
Throws | |
---|---|
NullPointerException |
if value == null . |
NumberFormatException |
if the length of value is zero.
|
BigInteger abs ()
Returns a BigInteger
whose value is the absolute value of this
.
Returns | |
---|---|
BigInteger |
BigInteger add (BigInteger value)
Returns a BigInteger
whose value is this + value
.
Parameters | |
---|---|
value |
BigInteger
|
Returns | |
---|---|
BigInteger |
BigInteger and (BigInteger value)
Returns a BigInteger
whose value is this & value
.
Implementation Note: Usage of this method is not recommended as the current implementation is not efficient.
Parameters | |
---|---|
value |
BigInteger :
value to be and'ed with this . |
Returns | |
---|---|
BigInteger |
Throws | |
---|---|
NullPointerException |
if value == null .
|
BigInteger andNot (BigInteger value)
Returns a BigInteger
whose value is this & ~value
.
Evaluating x.andNot(value)
returns the same result as x.and(value.not())
.
Implementation Note: Usage of this method is not recommended as the current implementation is not efficient.
Parameters | |
---|---|
value |
BigInteger :
value to be not'ed and then and'ed with this . |
Returns | |
---|---|
BigInteger |
Throws | |
---|---|
NullPointerException |
if value == null .
|
int bitCount ()
Returns the number of bits in the two's complement representation of
this
which differ from the sign bit. If this
is negative,
the result is equivalent to the number of bits set in the two's
complement representation of -this - 1
.
Use bitLength(0)
to find the length of the binary value in
bits.
Implementation Note: Usage of this method is not recommended as the current implementation is not efficient.
Returns | |
---|---|
int |
int bitLength ()
Returns the length of the value's two's complement representation without leading zeros for positive numbers / without leading ones for negative values.
The two's complement representation of this
will be at least
bitLength() + 1
bits long.
The value will fit into an int
if bitLength() < 32
or
into a long
if bitLength() < 64
.
Returns | |
---|---|
int |
the length of the minimal two's complement representation for
this without the sign bit.
|
BigInteger clearBit (int n)
Returns a BigInteger
which has the same binary representation
as this
but with the bit at position n cleared. The result is
equivalent to this & ~pow(2, n)
.
Implementation Note: Usage of this method is not recommended as the current implementation is not efficient.
Parameters | |
---|---|
n |
int :
position where the bit in this has to be cleared. |
Returns | |
---|---|
BigInteger |
Throws | |
---|---|
ArithmeticException |
if n < 0 .
|
int compareTo (BigInteger value)
Compares this BigInteger
with value
. Returns -1
if this < value
, 0
if this == value
and 1
if this > value
, .
Parameters | |
---|---|
value |
BigInteger :
value to be compared with this . |
Returns | |
---|---|
int |
Throws | |
---|---|
NullPointerException |
if value == null .
|
BigInteger divide (BigInteger divisor)
Returns a BigInteger
whose value is this / divisor
.
Parameters | |
---|---|
divisor |
BigInteger :
value by which this is divided. |
Returns | |
---|---|
BigInteger |
this / divisor . |
Throws | |
---|---|
NullPointerException |
if divisor == null . |
ArithmeticException |
if divisor == 0 .
|
BigInteger[] divideAndRemainder (BigInteger divisor)
Returns a two element BigInteger
array containing
this / divisor
at index 0 and this % divisor
at index 1.
Parameters | |
---|---|
divisor |
BigInteger :
value by which this is divided. |
Returns | |
---|---|
BigInteger[] |
Throws | |
---|---|
NullPointerException |
if divisor == null . |
ArithmeticException |
if divisor == 0 . |
See also:
double doubleValue ()
Returns this BigInteger
as a double. If this
is too big
to be represented as a double, then Double.POSITIVE_INFINITY
or
Double.NEGATIVE_INFINITY
is returned. Note that not all integers
in the range [-Double.MAX_VALUE, Double.MAX_VALUE]
can be exactly
represented as a double.
Returns | |
---|---|
double |
the numeric value represented by this object after conversion
to type double .
|
boolean equals (Object x)
Indicates whether some other object is "equal to" this one.
The equals
method implements an equivalence relation
on non-null object references:
x
, x.equals(x)
should return
true
.
x
and y
, x.equals(y)
should return true
if and only if
y.equals(x)
returns true
.
x
, y
, and z
, if
x.equals(y)
returns true
and
y.equals(z)
returns true
, then
x.equals(z)
should return true
.
x
and y
, multiple invocations of
x.equals(y)
consistently return true
or consistently return false
, provided no
information used in equals
comparisons on the
objects is modified.
x
,
x.equals(null)
should return false
.
The equals
method for class Object
implements
the most discriminating possible equivalence relation on objects;
that is, for any non-null reference values x
and
y
, this method returns true
if and only
if x
and y
refer to the same object
(x == y
has the value true
).
Note that it is generally necessary to override the hashCode
method whenever this method is overridden, so as to maintain the
general contract for the hashCode
method, which states
that equal objects must have equal hash codes.
Parameters | |
---|---|
x |
Object :
the reference object with which to compare. |
Returns | |
---|---|
boolean |
true if this object is the same as the obj
argument; false otherwise. |
BigInteger flipBit (int n)
Returns a BigInteger
which has the same binary representation
as this
but with the bit at position n flipped. The result is
equivalent to this ^ pow(2, n)
.
Implementation Note: Usage of this method is not recommended as the current implementation is not efficient.
Parameters | |
---|---|
n |
int :
position where the bit in this has to be flipped. |
Returns | |
---|---|
BigInteger |
Throws | |
---|---|
ArithmeticException |
if n < 0 .
|
float floatValue ()
Returns this BigInteger
as a float. If this
is too big to
be represented as a float, then Float.POSITIVE_INFINITY
or
Float.NEGATIVE_INFINITY
is returned. Note that not all integers
in the range [-Float.MAX_VALUE, Float.MAX_VALUE]
can be exactly
represented as a float.
Returns | |
---|---|
float |
the numeric value represented by this object after conversion
to type float .
|
BigInteger gcd (BigInteger value)
Returns a BigInteger
whose value is greatest common divisor
of this
and value
. If this == 0
and value == 0
then zero is returned, otherwise the result is positive.
Parameters | |
---|---|
value |
BigInteger :
value with which the greatest common divisor is computed. |
Returns | |
---|---|
BigInteger |
Throws | |
---|---|
NullPointerException |
if value == null .
|
int getLowestSetBit ()
Returns the position of the lowest set bit in the two's complement
representation of this BigInteger
. If all bits are zero (this==0)
then -1 is returned as result.
Implementation Note: Usage of this method is not recommended as the current implementation is not efficient.
Returns | |
---|---|
int |
int hashCode ()
Returns a hash code value for the object. This method is
supported for the benefit of hash tables such as those provided by
HashMap
.
The general contract of hashCode
is:
hashCode
method
must consistently return the same integer, provided no information
used in equals
comparisons on the object is modified.
This integer need not remain consistent from one execution of an
application to another execution of the same application.
equals(Object)
method, then calling the hashCode
method on each of
the two objects must produce the same integer result.
equals(java.lang.Object)
method, then calling the hashCode
method on each of the
two objects must produce distinct integer results. However, the
programmer should be aware that producing distinct integer results
for unequal objects may improve the performance of hash tables.
As much as is reasonably practical, the hashCode method defined by
class Object
does return distinct integers for distinct
objects. (This is typically implemented by converting the internal
address of the object into an integer, but this implementation
technique is not required by the
JavaTM programming language.)
Returns | |
---|---|
int |
a hash code value for this object. |
int intValue ()
Returns this BigInteger
as an int value. If this
is too
big to be represented as an int, then this % (1 << 32)
is
returned.
Returns | |
---|---|
int |
the numeric value represented by this object after conversion
to type int .
|
boolean isProbablePrime (int certainty)
Tests whether this BigInteger
is probably prime. If true
is returned, then this is prime with a probability greater than
1 - 1/2<sup>certainty</sup>)
. If false
is returned, then this
is definitely composite. If the argument certainty
<= 0, then
this method returns true.
Parameters | |
---|---|
certainty |
int :
tolerated primality uncertainty. |
Returns | |
---|---|
boolean |
true , if this is probably prime, false
otherwise.
|
long longValue ()
Returns this BigInteger
as a long value. If this
is too
big to be represented as a long, then this % pow(2, 64)
is
returned.
Returns | |
---|---|
long |
the numeric value represented by this object after conversion
to type long .
|
BigInteger max (BigInteger value)
Returns the maximum of this BigInteger
and value
.
Parameters | |
---|---|
value |
BigInteger :
value to be used to compute the maximum with this |
Returns | |
---|---|
BigInteger |
Throws | |
---|---|
NullPointerException |
if value == null
|
BigInteger min (BigInteger value)
Returns the minimum of this BigInteger
and value
.
Parameters | |
---|---|
value |
BigInteger :
value to be used to compute the minimum with this . |
Returns | |
---|---|
BigInteger |
Throws | |
---|---|
NullPointerException |
if value == null .
|
BigInteger mod (BigInteger m)
Returns a BigInteger
whose value is this mod m
. The
modulus m
must be positive. The result is guaranteed to be in the
interval [0, m)
(0 inclusive, m exclusive). The behavior of this
function is not equivalent to the behavior of the % operator defined for
the built-in int
's.
Parameters | |
---|---|
m |
BigInteger :
the modulus. |
Returns | |
---|---|
BigInteger |
this mod m . |
Throws | |
---|---|
NullPointerException |
if m == null . |
ArithmeticException |
if m < 0 .
|
BigInteger modInverse (BigInteger m)
Returns a BigInteger
whose value is 1/this mod m
. The
modulus m
must be positive. The result is guaranteed to be in the
interval [0, m)
(0 inclusive, m exclusive). If this
is
not relatively prime to m, then an exception is thrown.
Parameters | |
---|---|
m |
BigInteger :
the modulus. |
Returns | |
---|---|
BigInteger |
Throws | |
---|---|
NullPointerException |
if m == null |
ArithmeticException |
if m < 0 or if this is not
relatively prime to m
|
BigInteger modPow (BigInteger exponent, BigInteger modulus)
Returns a BigInteger
whose value is pow(this, exponent) mod modulus
. The modulus must be positive. The
result is guaranteed to be in the interval [0, modulus)
.
If the exponent is negative, then
pow(this.modInverse(modulus), -exponent) mod modulus
is computed.
The inverse of this only exists if this
is relatively prime to the modulus,
otherwise an exception is thrown.
Parameters | |
---|---|
exponent |
BigInteger
|
modulus |
BigInteger
|
Returns | |
---|---|
BigInteger |
Throws | |
---|---|
NullPointerException |
if modulus == null or exponent == null . |
ArithmeticException |
if modulus < 0 or if exponent < 0 and
not relatively prime to modulus .
|
BigInteger multiply (BigInteger value)
Returns a BigInteger
whose value is this * value
.
Parameters | |
---|---|
value |
BigInteger
|
Returns | |
---|---|
BigInteger |
Throws | |
---|---|
NullPointerException |
if value == null .
|
BigInteger negate ()
Returns a BigInteger
whose value is the -this
.
Returns | |
---|---|
BigInteger |
BigInteger nextProbablePrime ()
Returns the smallest integer x > this
which is probably prime as
a BigInteger
instance. The probability that the returned BigInteger
is prime is greater than 1 - 1/2<sup>100</sup>
.
Returns | |
---|---|
BigInteger |
smallest integer > this which is probably prime. |
Throws | |
---|---|
ArithmeticException |
if this < 0 .
|
BigInteger not ()
Returns a BigInteger
whose value is ~this
. The result
of this operation is -this-1
.
Implementation Note: Usage of this method is not recommended as the current implementation is not efficient.
Returns | |
---|---|
BigInteger |
BigInteger or (BigInteger value)
Returns a BigInteger
whose value is this | value
.
Implementation Note: Usage of this method is not recommended as the current implementation is not efficient.
Parameters | |
---|---|
value |
BigInteger :
value to be or'ed with this . |
Returns | |
---|---|
BigInteger |
Throws | |
---|---|
NullPointerException |
if value == null .
|
BigInteger pow (int exp)
Returns a BigInteger
whose value is pow(this, exp)
.
Parameters | |
---|---|
exp |
int
|
Returns | |
---|---|
BigInteger |
Throws | |
---|---|
ArithmeticException |
if exp < 0 .
|
BigInteger probablePrime (int bitLength, Random random)
Returns a random positive BigInteger
instance in the range [0, pow(2, bitLength)-1]
which is probably prime. The probability that
the returned BigInteger
is prime is greater than 1 - 1/2<sup>100</sup>)
.
Parameters | |
---|---|
bitLength |
int :
length of the new BigInteger in bits. |
random |
Random
|
Returns | |
---|---|
BigInteger |
probably prime random BigInteger instance. |
Throws | |
---|---|
IllegalArgumentException |
if bitLength < 2 .
|
BigInteger remainder (BigInteger divisor)
Returns a BigInteger
whose value is this % divisor
.
Regarding signs this methods has the same behavior as the % operator on
ints: the sign of the remainder is the same as the sign of this.
Parameters | |
---|---|
divisor |
BigInteger :
value by which this is divided. |
Returns | |
---|---|
BigInteger |
Throws | |
---|---|
NullPointerException |
if divisor == null . |
ArithmeticException |
if divisor == 0 .
|
BigInteger setBit (int n)
Returns a BigInteger
which has the same binary representation
as this
but with the bit at position n set. The result is
equivalent to this | pow(2, n)
.
Implementation Note: Usage of this method is not recommended as the current implementation is not efficient.
Parameters | |
---|---|
n |
int :
position where the bit in this has to be set. |
Returns | |
---|---|
BigInteger |
Throws | |
---|---|
ArithmeticException |
if n < 0 .
|
BigInteger shiftLeft (int n)
Returns a BigInteger
whose value is this << n
. The
result is equivalent to this * pow(2, n)
if n >= 0. The shift
distance may be negative which means that this
is shifted right.
The result then corresponds to floor(this / pow(2, -n))
.
Implementation Note: Usage of this method on negative values is not recommended as the current implementation is not efficient.
Parameters | |
---|---|
n |
int :
shift distance. |
Returns | |
---|---|
BigInteger |
this << n if n >= 0 ; this >> (-n) .
otherwise
|
BigInteger shiftRight (int n)
Returns a BigInteger
whose value is this >> n
. For
negative arguments, the result is also negative. The shift distance may
be negative which means that this
is shifted left.
Implementation Note: Usage of this method on negative values is not recommended as the current implementation is not efficient.
Parameters | |
---|---|
n |
int :
shift distance |
Returns | |
---|---|
BigInteger |
this >> n if n >= 0 ; this << (-n)
otherwise
|
int signum ()
Returns the sign of this BigInteger
.
Returns | |
---|---|
int |
-1 if this < 0 , 0 if this == 0 ,
1 if this > 0 .
|
BigInteger subtract (BigInteger value)
Returns a BigInteger
whose value is this - value
.
Parameters | |
---|---|
value |
BigInteger
|
Returns | |
---|---|
BigInteger |
boolean testBit (int n)
Tests whether the bit at position n in this
is set. The result is
equivalent to this & pow(2, n) != 0
.
Implementation Note: Usage of this method is not recommended as the current implementation is not efficient.
Parameters | |
---|---|
n |
int :
position where the bit in this has to be inspected. |
Returns | |
---|---|
boolean |
Throws | |
---|---|
ArithmeticException |
if n < 0 .
|
byte[] toByteArray ()
Returns the two's complement representation of this BigInteger
in
a byte array.
Returns | |
---|---|
byte[] |
String toString ()
Returns a string representation of this BigInteger
in decimal
form.
Returns | |
---|---|
String |
a string representation of the object. |
String toString (int radix)
Returns a string containing a string representation of this BigInteger
with base radix. If radix < Character.MIN_RADIX
or
radix > Character.MAX_RADIX
then a decimal representation is
returned. The characters of the string representation are generated with
method Character.forDigit
.
Parameters | |
---|---|
radix |
int :
base to be used for the string representation.
|
Returns | |
---|---|
String |
BigInteger valueOf (long value)
Returns a BigInteger
whose value is equal to value
.
Parameters | |
---|---|
value |
long
|
Returns | |
---|---|
BigInteger |
BigInteger xor (BigInteger value)
Returns a BigInteger
whose value is this ^ value
.
Implementation Note: Usage of this method is not recommended as the current implementation is not efficient.
Parameters | |
---|---|
value |
BigInteger :
value to be xor'ed with this |
Returns | |
---|---|
BigInteger |
Throws | |
---|---|
NullPointerException |
if value == null
|