Serializable, Comparable<BigInteger>
public class BigInteger extends Number implements Comparable<BigInteger>
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. 
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 since this operation only makes sense for a fixed sized word and not for a representation conceptually having an infinite number of leading virtual sign bits. 
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 arbitrarily large abstraction provided by this class ensures that conceptually 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. BigInteger must support values in the range -2Integer.MAX_VALUE (exclusive) to +2Integer.MAX_VALUE (exclusive) and may support values outside of that range. An ArithmeticException is thrown when a BigInteger constructor or method would generate a value outside of the supported range. The range of probable prime values is limited and may be less than the full supported positive range of BigInteger. The range must be at least 1 to 2500000000.
ArithmeticException when the result is out of the supported range of -2Integer.MAX_VALUE (exclusive) to +2Integer.MAX_VALUE (exclusive).| Modifier and Type | Field | Description | 
|---|---|---|
| static final BigInteger | ONE | The BigInteger constant one. | 
| static final BigInteger | TEN | The BigInteger constant ten. | 
| static final BigInteger | TWO | The BigInteger constant two. | 
| static final BigInteger | ZERO | The BigInteger constant zero. | 
| Constructor | Description | 
|---|---|
| BigInteger | Translates a byte array containing the two's-complement binary representation of a BigInteger into a BigInteger. | 
| BigInteger | Translates a byte sub-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 | 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 (2 numBits- 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. | 
| Modifier and Type | Method | Description | 
|---|---|---|
| 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. | 
| byte | byteValueExact() | Converts this  BigIntegerto abyte, checking for lost information. | 
| 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)andabs(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. | 
| int | intValueExact() | Converts this  BigIntegerto anint, checking for lost information. | 
| boolean | isProbablePrime | Returns  trueif this BigInteger is probably prime,falseif it's definitely composite. | 
| long | longValue() | Converts this BigInteger to a  long. | 
| long | longValueExact() | Converts this  BigIntegerto along, checking for lost information. | 
| 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-1mod 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  BigIntegerthat is probably prime. | 
| BigInteger | not() | Returns a BigInteger whose value is  (~this). | 
| BigInteger | or | Returns a BigInteger whose value is  (this | val). | 
| BigInteger | parallelMultiply | 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). | 
| short | shortValueExact() | Converts this  BigIntegerto ashort, checking for lost information. | 
| int | signum() | Returns the signum function of this BigInteger. | 
| BigInteger | sqrt() | Returns the integer square root of this BigInteger. | 
| BigInteger[] | sqrtAndRemainder() | Returns an array of two BigIntegers containing the integer square root  softhisand its remainderthis - s*s, respectively. | 
| BigInteger | subtract | Returns a BigInteger whose value is  (this - val). | 
| boolean | testBit | Returns  trueif 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). | 
byteValue, shortValue
public static final BigInteger ZERO
public static final BigInteger ONE
public static final BigInteger TWO
public static final BigInteger TEN
public BigInteger(byte[] val, int off, int len)
off. The val array is assumed to be unchanged for the duration of the constructor call. An IndexOutOfBoundsException is thrown if the length of the array val is non-zero and either off is negative, len is negative, or off+len is greater than the length of val.val - byte array containing a sub-array which is the big-endian two's-complement binary representation of a BigInteger.off - the start offset of the binary representation.len - the number of bytes to use.NumberFormatException - val is zero bytes long.IndexOutOfBoundsException - if the provided array offset and length would cause an index into the byte array to be negative or greater than or equal to the array length.public BigInteger(byte[] val)
val array is assumed to be unchanged for the duration of the constructor call.val - big-endian two's-complement binary representation of a BigInteger.NumberFormatException - val is zero bytes long.public BigInteger(int signum, byte[] magnitude, int off, int len)
off. A zero value of the length len is permissible, and will result in a BigInteger value of 0, whether signum is -1, 0 or 1. The magnitude array is assumed to be unchanged for the duration of the constructor call. An IndexOutOfBoundsException is thrown if the length of the array magnitude is non-zero and either off is negative, len is negative, or off+len is greater than the length of 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.off - the start offset of the binary representation.len - the number of bytes to use.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.IndexOutOfBoundsException - if the provided array offset and length would cause an index into the byte array to be negative or greater than or equal to the array length.public BigInteger(int signum, byte[] magnitude)
magnitude array is assumed to be unchanged for the duration of the constructor call.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)
Character.digit. The String may not contain any extraneous characters (whitespace, for example).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.public BigInteger(String val)
Character.digit. The String may not contain any extraneous characters (whitespace, for example).val - decimal String representation of BigInteger.NumberFormatException - val is not a valid representation of a BigInteger.public BigInteger(int numBits, Random rnd)
numBits - 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.numBits - maximum bitLength of the new BigInteger.rnd - source of randomness to be used in computing the new BigInteger.IllegalArgumentException - numBits is negative.public BigInteger(int bitLength, int certainty, Random rnd)
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 or bitLength is too large.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.bitLength bits that is probably primeArithmeticException - bitLength < 2 or bitLength is too large.public BigInteger nextProbablePrime()
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.this, this method could consume a large amount of memory, up to exhaustion of available heap space, or could run for a long time.BigInteger that is probably prime.ArithmeticException - this < 0 or this is too large.public static BigInteger valueOf(long val)
long.long) constructor because it allows for reuse of frequently used BigIntegers.val - value of the BigInteger to return.public BigInteger add(BigInteger val)
(this + val).val - value to be added to this BigInteger.this + valpublic BigInteger subtract(BigInteger val)
(this - val).val - value to be subtracted from this BigInteger.this - valpublic BigInteger multiply(BigInteger val)
(this * val).val == this.val - value to be multiplied by this BigInteger.this * valpublic BigInteger parallelMultiply(BigInteger val)
(this * val). When both this and val are large, typically in the thousands of bits, parallel multiply might be used. This method returns the exact same mathematical result as multiply(java.math.BigInteger).val == this., Compared to multiply(java.math.BigInteger), an implementation's parallel multiplication algorithm would typically use more CPU resources to compute the result faster, and may do so with a slight increase in memory consumption.val - value to be multiplied by this BigInteger.this * valpublic BigInteger divide(BigInteger val)
(this / val).val - value by which this BigInteger is to be divided.this / valArithmeticException - if val is zero.public BigInteger[] divideAndRemainder(BigInteger val)
(this / val) followed by (this % val).val - value by which this BigInteger is to be divided, and the remainder computed.(this / val) is the initial element, and the remainder (this % val) is the final element.ArithmeticException - if val is zero.public BigInteger remainder(BigInteger val)
(this % val).val - value by which this BigInteger is to be divided, and the remainder computed.this % valArithmeticException - if val is zero.public BigInteger pow(int exponent)
(thisexponent). Note that exponent is an integer rather than a BigInteger.exponent - exponent to which this BigInteger is to be raised.thisexponentArithmeticException - exponent is negative. (This would cause the operation to yield a non-integer value.)public BigInteger sqrt()
n is the largest mathematical integer s such that s*s <= n. It is equal to the value of floor(sqrt(n)), where sqrt(n) denotes the real square root of n treated as a real. Note that the integer square root will be less than the real square root if the latter is not representable as an integral value.this
ArithmeticException - if this is negative. (The square root of a negative integer val is (i * sqrt(-val)) where i is the imaginary unit and is equal to sqrt(-1).)public BigInteger[] sqrtAndRemainder()
s of this and its remainder this - s*s, respectively.ArithmeticException - if this is negative. (The square root of a negative integer val is (i * sqrt(-val)) where i is the imaginary unit and is equal to sqrt(-1).)public BigInteger gcd(BigInteger val)
abs(this) and abs(val). Returns 0 if this == 0 && val == 0.val - value with which the GCD is to be computed.GCD(abs(this), abs(val))public BigInteger abs()
abs(this)public BigInteger negate()
(-this).-thispublic int signum()
public BigInteger mod(BigInteger m)
(this mod m). This method differs from remainder in that it always returns a non-negative BigInteger.m - the modulus.this mod mArithmeticException - m ≤ 0public BigInteger modPow(BigInteger exponent, BigInteger m)
(thisexponent mod m). (Unlike pow, this method permits negative exponents.)exponent - the exponent.m - the modulus.thisexponent mod mArithmeticException - m ≤ 0 or the exponent is negative and this BigInteger is not relatively prime to m.public BigInteger modInverse(BigInteger m)
(this-1 mod m).m - the modulus.this-1 mod m.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)
(this << n). The shift distance, n, may be negative, in which case this method performs a right shift. (Computes floor(this * 2n).)n - shift distance, in bits.this << npublic BigInteger shiftRight(int n)
(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 / 2n).)n - shift distance, in bits.this >> npublic BigInteger and(BigInteger val)
(this & val). (This method returns a negative BigInteger if and only if this and val are both negative.)val - value to be AND'ed with this BigInteger.this & valpublic BigInteger or(BigInteger val)
(this | val). (This method returns a negative BigInteger if and only if either this or val is negative.)val - value to be OR'ed with this BigInteger.this | valpublic BigInteger xor(BigInteger val)
(this ^ val). (This method returns a negative BigInteger if and only if exactly one of this and val are negative.)val - value to be XOR'ed with this BigInteger.this ^ valpublic BigInteger not()
(~this). (This method returns a negative value if and only if this BigInteger is non-negative.)~thispublic BigInteger andNot(BigInteger val)
(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 & ~valpublic boolean testBit(int n)
true if and only if the designated bit is set. (Computes ((this & (1<<n)) != 0).)n - index of bit to test.true if and only if the designated bit is set.ArithmeticException - n is negative.public BigInteger setBit(int n)
(this | (1<<n)).)n - index of bit to set.this | (1<<n)ArithmeticException - n is negative.public BigInteger clearBit(int n)
(this & ~(1<<n)).)n - index of bit to clear.this & ~(1<<n)ArithmeticException - n is negative.public BigInteger flipBit(int n)
(this ^ (1<<n)).)n - index of bit to flip.this ^ (1<<n)ArithmeticException - n is negative.public int getLowestSetBit()
(this == 0? -1 : log2(this & -this)).)public int bitLength()
0. (Computes (ceil(log2(this < 0 ? -this : this+1))).)public int bitCount()
public boolean isProbablePrime(int certainty)
true if this BigInteger is probably prime, false if it's definitely composite. If certainty is ≤ 0, true is returned.this and certainty, this method could consume a large amount of memory, up to exhaustion of available heap space, or could run for a long time.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.true if this BigInteger is probably prime, false if it's definitely composite.ArithmeticException - this is too large.public int compareTo(BigInteger val)
 (x.compareTo(y) <op> 0), where <op> is one of the six comparison operators.compareTo in interface Comparable<BigInteger>
val - BigInteger to which this BigInteger is to be compared.val.public boolean equals(Object x)
public BigInteger min(BigInteger val)
val.val - value with which the minimum is to be computed.val. If they are equal, either may be returned.public BigInteger max(BigInteger val)
val.val - value with which the maximum is to be computed.val. If they are equal, either may be returned.public int hashCode()
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 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.)radix - radix of the String representation.public String toString()
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.)public byte[] toByteArray()
(ceil((this.bitLength() +
 1)/8)). (This representation is compatible with the (byte[]) constructor.)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 low-order 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 low-order 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 as 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 as 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.public long longValueExact()
BigInteger to a long, checking for lost information. If the value of this BigInteger is out of the range of the long type, then an ArithmeticException is thrown.BigInteger converted to a long.ArithmeticException - if the value of this will not exactly fit in a long.public int intValueExact()
BigInteger to an int, checking for lost information. If the value of this BigInteger is out of the range of the int type, then an ArithmeticException is thrown.BigInteger converted to an int.ArithmeticException - if the value of this will not exactly fit in an int.public short shortValueExact()
BigInteger to a short, checking for lost information. If the value of this BigInteger is out of the range of the short type, then an ArithmeticException is thrown.BigInteger converted to a short.ArithmeticException - if the value of this will not exactly fit in a short.public byte byteValueExact()
BigInteger to a byte, checking for lost information. If the value of this BigInteger is out of the range of the byte type, then an ArithmeticException is thrown.BigInteger converted to a byte.ArithmeticException - if the value of this will not exactly fit in a byte.
    © 1993, 2023, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
    https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/math/BigInteger.html