APIdia Launch Partner
Top Description Inners Fields Constructors Methods
java.math

public Class BigInteger

extends Number
implements Comparable<BigInteger>
Class Inheritance
All Implemented Interfaces
java.lang.Comparable
Imports
java.io.IOException, .InvalidObjectException, .ObjectInputStream, .ObjectOutputStream, .ObjectStreamField, .ObjectStreamException, java.util.Arrays, .Objects, .Random, java.util.concurrent.ForkJoinPool, .ForkJoinWorkerThread, .RecursiveTask, .ThreadLocalRandom, jdk.internal.math.DoubleConsts, .FloatConsts, jdk.internal.util.ArraysSupport, jdk.internal.vm.annotation.ForceInline, .IntrinsicCandidate, .Stable

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.

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.

API Note

As BigInteger values are arbitrary precision integers, the algorithmic complexity of the methods of this class varies and may be superlinear in the size of the input. For example, a method like intValue() would be expected to run in O(1), that is constant time, since with the current internal representation only a fixed-size component of the BigInteger needs to be accessed to perform the conversion to int. In contrast, a method like not() would be expected to run in O(n) time where n is the size of the BigInteger in bits, that is, to run in time proportional to the size of the input. For multiplying two BigInteger values of size n, a naive multiplication algorithm would run in time O(n2) and theoretical results indicate a multiplication algorithm for numbers using this category of representation must run in at least O(n log n). Common multiplication algorithms between the bounds of the naive and theoretical cases include the Karatsuba multiplication (O(n1.585)) and 3-way Toom-Cook multiplication (O(n1.465)).

A particular implementation of multiply is free to switch between different algorithms for different inputs, such as to improve actual running time to produce the product by using simpler algorithms for smaller inputs even if the simpler algorithm has a larger asymptotic complexity.

Operations may also allocate and compute on intermediate results, potentially those allocations may be as large as in proportion to the running time of the algorithm.

Users of BigInteger concerned with bounding the running time or space of operations can screen out BigInteger values above a chosen magnitude.

Implementation Note

In the reference implementation, BigInteger constructors and operations throw ArithmeticException when the result is out of the supported range of -2Integer.MAX_VALUE (exclusive) to +2Integer.MAX_VALUE (exclusive).

Authors
Josh Bloch, Michael McCloskey, Alan Eliasen, Timothy Buktu
Since
1.1
Java Language Specification
4.2.2 Integer Operations
See Also
BigDecimal

Nested and Inner Type Summary

Modifier and TypeClass and Description
private abstract static class
private static class

Field Summary

Modifier and TypeField and Description
private int
bitCountPlusOne

One plus the bitCount of this BigInteger.

private static long[]
pack-priv static int[]
pack-priv static final int
BURNIKEL_ZIEGLER_OFFSET

The offset value for using Burnikel-Ziegler division.

pack-priv static final int
BURNIKEL_ZIEGLER_THRESHOLD

The threshold value for using Burnikel-Ziegler division.

private static final int
private static int[]
private static int[]
private static int[]
private static final int
KARATSUBA_SQUARE_THRESHOLD

The threshold value for using Karatsuba squaring.

private static final int
KARATSUBA_THRESHOLD

The threshold value for using Karatsuba multiplication.

private static final double
LOG_TWO

The natural log of 2.

private static final double[]
logCache

The cache of logarithms of radices for base conversion.

pack-priv static final long
LONG_MASK

This mask is used to obtain the value of an int as if it were unsigned.

private static BigInteger[]
private int
lowestSetBitPlusTwo

Two plus the lowest set bit of this BigInteger.

pack-priv final int[]
mag

The magnitude of this BigInteger, in big-endian order: the zeroth element of this array is the most-significant int of the magnitude.

private int
magBitLengthPlusOne

One plus the bitLength of the magnitude of this BigInteger.

private static final int
MAX_CONSTANT

Initialize static constant array when class is loaded.

private static final int
MAX_MAG_LENGTH

This constant limits mag.length of BigIntegers to the supported range.

private static final int
MONTGOMERY_INTRINSIC_THRESHOLD

The threshold for using an intrinsic version of implMontgomeryXXX to perform Montgomery multiplication.

private static final int
MULTIPLY_SQUARE_THRESHOLD

The threshold value for using squaring code to perform multiplication of a BigInteger instance by itself.

private static final BigInteger
NEGATIVE_ONE

The BigInteger constant -1.

private static final BigInteger[]
private static int
private int
numberOfTrailingZeroIntsPlusTwo

Two plus the index of the lowest-order int in the magnitude of this BigInteger that contains a nonzero int.

public static final BigInteger
ONE

The BigInteger constant one.

private static final BigInteger[]
private static volatile BigInteger[][]
powerCache

The cache of powers of each radix.

private static final int
PRIME_SEARCH_BIT_LENGTH_LIMIT

Bit lengths larger than this constant can cause overflow in searchLen calculation and in BitSieve.singleSearch method.

private static final int
SCHOENHAGE_BASE_CONVERSION_THRESHOLD

The threshold value for using Schoenhage recursive base conversion.

private static final ObjectStreamField[]
serialPersistentFields

Serializable fields for BigInteger.

private static final long
serialVersionUID

Hides java.lang.Number.serialVersionUID.

use serialVersionUID from JDK 1.1.
pack-priv final int
signum

The signum of this BigInteger: -1 for negative, 0 for zero, or 1 for positive.

private static final BigInteger
private static final int
public static final BigInteger
TEN

The BigInteger constant ten.

private static final int
TOOM_COOK_SQUARE_THRESHOLD

The threshold value for using Toom-Cook squaring.

private static final int
TOOM_COOK_THRESHOLD

The threshold value for using 3-way Toom-Cook multiplication.

public static final BigInteger
TWO

The BigInteger constant two.

public static final BigInteger
ZERO

The BigInteger constant zero.

private static final String

Constructor Summary

AccessConstructor and Description
public
BigInteger(byte[]
byte array containing a sub-array which is the big-endian two's-complement binary representation of a BigInteger.
val
,
int
the start offset of the binary representation.
off
,
int
the number of bytes to use.
len
)

Translates a byte sub-array containing the two's-complement binary representation of a BigInteger into a BigInteger.

public
BigInteger(byte[]
big-endian two's-complement binary representation of a BigInteger.
val
)

Translates a byte array containing the two's-complement binary representation of a BigInteger into a BigInteger.

private
BigInteger(int[] val)

This private constructor translates an int array containing the two's-complement binary representation of a BigInteger into a BigInteger.

public
BigInteger(int
signum of the number (-1 for negative, 0 for zero, 1 for positive).
signum
,
byte[]
big-endian binary representation of the magnitude of the number.
magnitude
,
int
the start offset of the binary representation.
off
,
int
the number of bytes to use.
len
)

Translates the sign-magnitude representation of a BigInteger into a BigInteger.

public
BigInteger(int
signum of the number (-1 for negative, 0 for zero, 1 for positive).
signum
,
byte[]
big-endian binary representation of the magnitude of the number.
magnitude
)

Translates the sign-magnitude representation of a BigInteger into a BigInteger.

private
BigInteger(int signum, int[] magnitude)

A constructor for internal use that translates the sign-magnitude representation of a BigInteger into a BigInteger.

public
BigInteger(String
String representation of BigInteger.
val
,
int
radix to be used in interpreting val.
radix
)

Translates the String representation of a BigInteger in the specified radix into a BigInteger.

pack-priv
BigInteger(char[] val, int sign, int len)

public
BigInteger(String
decimal String representation of BigInteger.
val
)

Translates the decimal String representation of a BigInteger into a BigInteger.

public
BigInteger(int
maximum bitLength of the new BigInteger.
numBits
,
Random
source of randomness to be used in computing the new BigInteger.
rnd
)

Constructs a randomly generated BigInteger, uniformly distributed over the range 0 to (2numBits - 1), inclusive.

public
BigInteger(int
bitLength of the returned BigInteger.
bitLength
,
int
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.
certainty
,
Random
source of random bits used to select candidates to be tested for primality.
rnd
)

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

pack-priv
BigInteger(int[] magnitude, int signum)

This internal constructor differs from its public cousin with the arguments reversed in two ways: it assumes that its arguments are correct, and it doesn't copy the magnitude array.

private
BigInteger(byte[] magnitude, int signum)

This private constructor is for internal use and assumes that its arguments are correct.

private
BigInteger(long mag, int signum)

Constructs a BigInteger with magnitude specified by the long, which may not be zero, and the signum specified by the int.

private
BigInteger(long val)

Constructs a BigInteger with the specified value, which may not be zero.

Method Summary

Modifier and TypeMethod and Description
public BigInteger

Returns:

abs(this)
abs
()

Returns a BigInteger whose value is the absolute value of this BigInteger.

public BigInteger

Returns:

this + val
add
(BigInteger
value to be added to this BigInteger.
val
)

Returns a BigInteger whose value is (this + val).

pack-priv BigInteger
add(long val)

Package private methods used by BigDecimal code to add a BigInteger with a long.

private static int[]
add(int[] x, long val)

Adds the contents of the int array x and long value val.

private static int[]
add(int[] x, int[] y)

Adds the contents of the int arrays x and y.

pack-priv static int
addOne(int[] a, int offset, int mlen, int carry)

Add one word to the number a mlen words into a.

public BigInteger

Returns:

this & val
and
(BigInteger
value to be AND'ed with this BigInteger.
val
)

Returns a BigInteger whose value is (this & val).

public BigInteger

Returns:

this & ~val
andNot
(BigInteger
value to be complemented and AND'ed with this BigInteger.
val
)

Returns a BigInteger whose value is (this & ~val).

public int

Returns:

number of bits in the two's complement representation of this BigInteger that differ from its sign bit.
bitCount
()

Returns the number of bits in the two's complement representation of this BigInteger that differ from its sign bit.

public int

Returns:

number of bits in the minimal two's-complement representation of this BigInteger, excluding a sign bit.
bitLength
()

Returns the number of bits in the minimal two's-complement representation of this BigInteger, excluding a sign bit.

pack-priv static int
bitLengthForInt(int n)

Package private method to return bit length for an integer.

pack-priv static int
bitLengthForLong(long n)

Package private method to return bit length for a long.

public byte

Returns:

this BigInteger converted to a byte.
byteValueExact
()

Converts this BigInteger to a byte, checking for lost information.

private void
checkRange()

Throws an ArithmeticException if the BigInteger would be out of the supported range.

private void
public BigInteger

Returns:

this & ~(1<<n)
clearBit
(int
index of bit to clear.
n
)

Returns a BigInteger whose value is equivalent to this BigInteger with the designated bit cleared.

pack-priv final int

Returns:

-1, 0 or 1 as this magnitude array is less than, equal to or greater than the magnitude array for the specified BigInteger's.
compareMagnitude
(BigInteger
BigInteger whose magnitude array to be compared.
val
)

Compares the magnitude array of this BigInteger with the specified BigInteger's.

pack-priv final int
compareMagnitude(long val)

Version of compareMagnitude that compares magnitude with long value.

public int

Returns:

-1, 0 or 1 as this BigInteger is numerically less than, equal to, or greater than val.
compareTo
(BigInteger
BigInteger to which this BigInteger is to be compared.
val
)

Implements java.lang.Comparable.compareTo.

Compares this BigInteger with the specified BigInteger.

private static void
destructiveMulAdd(int[] x, int y, int z)

public BigInteger

Returns:

this / val
divide
(BigInteger
value by which this BigInteger is to be divided.
val
)

Returns a BigInteger whose value is (this / val).

public BigInteger[]

Returns:

an array of two BigIntegers: the quotient (this / val) is the initial element, and the remainder (this % val) is the final element.
divideAndRemainder
(BigInteger
value by which this BigInteger is to be divided, and the remainder computed.
val
)

Returns an array of two BigIntegers containing (this / val) followed by (this % val).

private BigInteger[]

Returns:

an array containing the quotient and remainder
divideAndRemainderBurnikelZiegler
(BigInteger
the divisor
val
)

Computes this / val and this % val using the Burnikel-Ziegler algorithm.

private BigInteger[]
private BigInteger

Returns:

this / val
divideBurnikelZiegler
(BigInteger
the divisor
val
)

Calculates this / val using the Burnikel-Ziegler algorithm.

private BigInteger

Returns:

this / val
divideKnuth
(BigInteger
value by which this BigInteger is to be divided.
val
)

Returns a BigInteger whose value is (this / val) using an O(n^2) algorithm from Knuth.

public double

Returns:

this BigInteger converted to a double.
doubleValue
()

Implements abstract java.lang.Number.doubleValue.

Converts this BigInteger to a double.

public boolean

Returns:

true if and only if the specified Object is a BigInteger whose value is numerically equal to this BigInteger.
equals
(Object
Object to which this BigInteger is to be compared.
x
)

Overrides java.lang.Object.equals.

Compares this BigInteger with the specified Object for equality.

private BigInteger
exactDivideBy3()

Does an exact division (that is, the remainder is known to be zero) of the specified number by 3.

private boolean
public BigInteger

Returns:

this ^ (1<<n)
flipBit
(int
index of bit to flip.
n
)

Returns a BigInteger whose value is equivalent to this BigInteger with the designated bit flipped.

public float

Returns:

this BigInteger converted to a float.
floatValue
()

Implements abstract java.lang.Number.floatValue.

Converts this BigInteger to a float.

public BigInteger

Returns:

GCD(abs(this), abs(val))
gcd
(BigInteger
value with which the GCD is to be computed.
val
)

Returns a BigInteger whose value is the greatest common divisor of abs(this) and abs(val).

private int
getInt(int n)

Returns the specified int of the little-endian two's complement representation (int 0 is the least significant).

private BigInteger
getLower(int n)

Returns a new BigInteger representing n lower ints of the number.

public int

Returns:

index of the rightmost one bit in this BigInteger.
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).

private static int
getPrimeSearchLen(int bitLength)

private static BigInteger
getRadixConversionCache(int radix, int exponent)

Returns the value radix^(2^exponent) from the cache.

private BigInteger
getToomSlice(int
The size of the lower-order bit slices.
lowerSize
,
int
The size of the higher-order bit slices.
upperSize
,
int
The index of which slice is requested, which must be a number from 0 to size-1. Slice 0 is the highest-order bits, and slice size-1 are the lowest-order bits. Slice 0 may be of different size than the other slices.
slice
,
int
The size of the larger integer array, used to align slices to the appropriate position when multiplying different-sized numbers.
fullsize
)

Returns a slice of a BigInteger for use in Toom-Cook multiplication.

private BigInteger
getUpper(int n)

Returns a new BigInteger representing mag.length-n upper ints of the number.

public int

Returns:

the hash code for this BigInteger
hashCode
()

Overrides java.lang.Object.hashCode.

Returns the hash code for this BigInteger.

private static int[]
implMontgomeryMultiply(int[] a, int[] b, int[] n, int len, long inv, int[] product)

private static void
implMontgomeryMultiplyChecks(int[] a, int[] b, int[] n, int len, int[] product)

private static int[]
implMontgomerySquare(int[] a, int[] n, int len, long inv, int[] product)

private static int
implMulAdd(int[] out, int[] in, int offset, int len, int k)

Java Runtime may use intrinsic for this method.

private static void
implMulAddCheck(int[] out, int[] in, int offset, int len, int k)

Parameters validation.

private static int[]
implMultiplyToLen(int[] x, int xlen, int[] y, int ylen, int[] z)

private static final int[]
implSquareToLen(int[] x, int len, int[] z, int zlen)

Java Runtime may use intrinsic for this method.

private static void
implSquareToLenChecks(int[] x, int len, int[] z, int zlen)

Parameters validation.

private static int
intArrayCmpToLen(int[] arg1, int[] arg2, int len)

private int
intLength()

Returns the length of the two's complement representation in ints, including space for at least one sign bit.

public int

Returns:

this BigInteger converted to an int.
intValue
()

Implements abstract java.lang.Number.intValue.

Converts this BigInteger to an int.

public int

Returns:

this BigInteger converted to an int.
intValueExact
()

Converts this BigInteger to an int, checking for lost information.

public boolean

Returns:

true if this BigInteger is probably prime, false if it's definitely composite.
isProbablePrime
(int
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.
certainty
)

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

private static int
jacobiSymbol(int p, BigInteger n)

Computes Jacobi(p,n).

pack-priv static int[]
javaIncrement(int[] val)

private static BigInteger
largePrime(int bitLength, int certainty, Random rnd)

Find a random number of the specified bitLength that is probably prime.

private static int[]
leftShift(int[] a, int len, int n)

Left shift int array a up to len by n bits.

public long

Returns:

this BigInteger converted to a long.
longValue
()

Implements abstract java.lang.Number.longValue.

Converts this BigInteger to a long.

public long

Returns:

this BigInteger converted to a long.
longValueExact
()

Converts this BigInteger to a long, checking for lost information.

private static BigInteger
private int
magBitLength()

Calculate bitlength of the magnitude of this BigInteger.

private byte[]
magSerializedForm()

Returns the mag array as an array of bytes.

private static int[]
makePositive(int b, byte[] a, int from, int len)

private static int[]
makePositive(int[] a)

Takes an array a representing a negative 2's-complement number and returns the minimal (no leading zero ints) unsigned whose value is -a.

private static int[]
materialize(int[] z, int len)

public BigInteger

Returns:

the BigInteger whose value is the greater of this and val. If they are equal, either may be returned.
max
(BigInteger
value with which the maximum is to be computed.
val
)

Returns the maximum of this BigInteger and val.

public BigInteger

Returns:

the BigInteger whose value is the lesser of this BigInteger and val. If they are equal, either may be returned.
min
(BigInteger
value with which the minimum is to be computed.
val
)

Returns the minimum of this BigInteger and val.

public BigInteger

Returns:

this mod m
mod
(BigInteger
the modulus.
m
)

Returns a BigInteger whose value is (this mod m).

private BigInteger
mod2(int p)

Returns a BigInteger whose value is this mod(2**p).

public BigInteger

Returns:

this-1 mod m.
modInverse
(BigInteger
the modulus.
m
)

Returns a BigInteger whose value is (this-1 mod m).

public BigInteger

Returns:

thisexponent mod m
modPow
(BigInteger
the exponent.
exponent
,
BigInteger
the modulus.
m
)

Returns a BigInteger whose value is (thisexponent mod m).

private BigInteger
modPow2(BigInteger exponent, int p)

Returns a BigInteger whose value is (this ** exponent) mod (2**p)

private static int[]
montgomeryMultiply(int[] a, int[] b, int[] n, int len, long inv, int[] product)

private static int[]
montgomerySquare(int[] a, int[] n, int len, long inv, int[] product)

private static int[]
montReduce(int[] n, int[] mod, int mlen, int inv)

Montgomery reduce n, modulo mod.

pack-priv static int
mulAdd(int[] out, int[] in, int offset, int len, int k)

Multiply an array by one word k and add to result, return the carry

public BigInteger

Returns:

this * val
multiply
(BigInteger
value to be multiplied by this BigInteger.
val
)

Returns a BigInteger whose value is (this * val).

private BigInteger

Returns:

this * val
multiply
(BigInteger
value to be multiplied by this BigInteger.
val
,
boolean
whether this is a recursive invocation
isRecursion
,
boolean
whether the multiply should be done in parallel
parallel
,
int depth)

Returns a BigInteger whose value is (this * val).

pack-priv BigInteger
multiply(long v)

Package private methods used by BigDecimal code to multiply a BigInteger with a long.

private static BigInteger
multiplyByInt(int[] x, int y, int sign)

private static BigInteger
multiplyKaratsuba(BigInteger x, BigInteger y)

Multiplies two BigIntegers using the Karatsuba multiplication algorithm.

private static int[]
multiplyToLen(int[] x, int xlen, int[] y, int ylen, int[] z)

Multiplies int arrays x and y to the specified lengths and places the result into z.

private static void
multiplyToLenCheck(int[] array, int length)

private static BigInteger
multiplyToomCook3(BigInteger a, BigInteger b, boolean parallel, int depth)

Multiplies two BigIntegers using a 3-way Toom-Cook multiplication algorithm.

public BigInteger

Returns:

-this
negate
()

Returns a BigInteger whose value is (-this).

public BigInteger

Returns:

the first integer greater than this BigInteger that is probably prime.
nextProbablePrime
()

Returns the first integer greater than this BigInteger that is probably prime.

public BigInteger

Returns:

~this
not
()

Returns a BigInteger whose value is (~this).

private int
numberOfTrailingZeroInts()

Returns the index of the int that contains the first nonzero int in the little-endian binary representation of the magnitude (int 0 is the least significant).

private BigInteger
oddModPow(BigInteger y, BigInteger z)

Returns a BigInteger whose value is x to the power of y mod z.

public BigInteger

Returns:

this | val
or
(BigInteger
value to be OR'ed with this BigInteger.
val
)

Returns a BigInteger whose value is (this | val).

private static void
padWithZeros(StringBuilder
The StringBuilder that will be appended to.
buf
,
int
The number of zeros to append.
numZeros
)

If numZeros > 0, appends that many zeros to the specified StringBuilder; otherwise, does nothing.

public BigInteger

Returns:

this * val
parallelMultiply
(BigInteger
value to be multiplied by this BigInteger.
val
)

Returns a BigInteger whose value is (this * val).

private int
parseInt(char[] source, int start, int end)

private boolean
passesLucasLehmer()

Returns true iff this BigInteger is a Lucas-Lehmer probable prime.

private boolean
passesMillerRabin(int iterations, Random rnd)

Returns true iff this BigInteger passes the specified number of Miller-Rabin tests.

public BigInteger

Returns:

thisexponent
pow
(int
exponent to which this BigInteger is to be raised.
exponent
)

Returns a BigInteger whose value is (thisexponent).

pack-priv boolean

Returns:

true if this BigInteger is probably prime, false if it's definitely composite.
primeToCertainty
(int
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.
certainty
,
Random random)

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

pack-priv static void
primitiveLeftShift(int[] a, int len, int n)

pack-priv static void
primitiveRightShift(int[] a, int len, int n)

public static BigInteger

Returns:

a BigInteger of bitLength bits that is probably prime
probablePrime
(int
bitLength of the returned BigInteger.
bitLength
,
Random
source of random bits used to select candidates to be tested for primality.
rnd
)

Returns a positive BigInteger that is probably prime, with the specified bitLength.

private static byte[]
randomBits(int numBits, Random rnd)

private void
readObject(ObjectInputStream
the stream being read.
s
)

Reconstitute the BigInteger instance from a stream (that is, deserialize it).

private void
readObjectNoData()

Serialization without data not supported for this class.

public BigInteger

Returns:

this % val
remainder
(BigInteger
value by which this BigInteger is to be divided, and the remainder computed.
val
)

Returns a BigInteger whose value is (this % val).

private BigInteger

Returns:

this % val
remainderBurnikelZiegler
(BigInteger
the divisor
val
)

Calculates this % val using the Burnikel-Ziegler algorithm.

private BigInteger
remainderKnuth(BigInteger val)

Long division

private static void
public BigInteger

Returns:

the integer nth root of this
rootn
(int
the root degree
n
)

Returns the integer nth root of this BigInteger.

public BigInteger[]

Returns:

an array of two BigIntegers with the integer nth root at offset 0 and the remainder at offset 1
rootnAndRemainder
(int
the root degree
n
)

Returns an array of two BigIntegers containing the integer nth root r of this and its remainder this - rn, respectively.

public BigInteger

Returns:

this | (1<<n)
setBit
(int
index of bit to set.
n
)

Returns a BigInteger whose value is equivalent to this BigInteger with the designated bit set.

public BigInteger

Returns:

this << n
shiftLeft
(int
shift distance, in bits.
n
)

Returns a BigInteger whose value is (this << n).

private static int[]

Returns:

mag << n
shiftLeft
(int[]
magnitude, the most-significant int (mag[0]) must be non-zero.
mag
,
int
unsigned shift distance, in bits.
n
)

Returns a magnitude array whose value is (mag << n).

private static void
shiftLeftImplWorker(int[] newArr, int[] oldArr, int newIdx, int shiftCount, int numIter)

public BigInteger

Returns:

this >> n
shiftRight
(int
shift distance, in bits.
n
)

Returns a BigInteger whose value is (this >> n).

private BigInteger

Returns:

this >> n
shiftRightImpl
(int
unsigned shift distance, in bits.
n
)

Returns a BigInteger whose value is (this >> n).

private static void
shiftRightImplWorker(int[] newArr, int[] oldArr, int newIdx, int shiftCount, int numIter)

public short

Returns:

this BigInteger converted to a short.
shortValueExact
()

Converts this BigInteger to a short, checking for lost information.

private int
private int
public int

Returns:

-1, 0 or 1 as the value of this BigInteger is negative, zero or positive.
signum
()

Returns the signum function of this BigInteger.

private static BigInteger
smallPrime(int bitLength, int certainty, Random rnd)

Find a random number of the specified bitLength that is probably prime.

private void
smallToString(int
The base to convert to.
radix
,
StringBuilder
The StringBuilder that will be appended to in place.
buf
,
int
The minimum number of digits to pad to.
digits
)

This method is used to perform toString when arguments are small.

public BigInteger

Returns:

the integer square root of this
sqrt
()

Returns the integer square root of this BigInteger.

public BigInteger[]

Returns:

an array of two BigIntegers with the integer square root at offset 0 and the remainder at offset 1
sqrtAndRemainder
()

Returns an array of two BigIntegers containing the integer square root s of this and its remainder this - s*s, respectively.

private BigInteger

Returns:

this2
square
()

Returns a BigInteger whose value is (this2).

private BigInteger

Returns:

this2
square
(boolean
whether this is a recursive invocation
isRecursion
,
boolean parallel, int depth)

Returns a BigInteger whose value is (this2).

private BigInteger
squareKaratsuba()

Squares a BigInteger using the Karatsuba squaring algorithm.

private static final int[]
squareToLen(int[] x, int len, int[] z)

Squares the contents of the int array x.

private BigInteger
squareToomCook3(boolean parallel, int depth)

Squares a BigInteger using the 3-way Toom-Cook squaring algorithm.

private static int[]
stripLeadingZeroBytes(byte[] a, int from, int len)

private static int[]
stripLeadingZeroBytes(int b, byte[] a, int from, int len)

private static int[]
stripLeadingZeroInts(int[] val, boolean trusted)

Returns the input array stripped of any leading zero ints.

private static int
subN(int[] a, int[] b, int len)

Subtracts two numbers of same length, returning borrow.

private static int[]
subtract(long val, int[] little)

private static int[]
subtract(int[] big, long val)

Subtracts the contents of the second argument (val) from the first (big).

public BigInteger

Returns:

this - val
subtract
(BigInteger
value to be subtracted from this BigInteger.
val
)

Returns a BigInteger whose value is (this - val).

private static int[]
subtract(int[] big, int[] little)

Subtracts the contents of the second int arrays (little) from the first (big).

public boolean

Returns:

true if and only if the designated bit is set.
testBit
(int
index of bit to test.
n
)

Returns true if and only if the designated bit is set.

public byte[]

Returns:

a byte array containing the two's-complement representation of this BigInteger.
toByteArray
()

Returns a byte array containing the two's-complement representation of this BigInteger.

private static int[]
toMagArray(long mag)

public String

Returns:

String representation of this BigInteger in the given radix.
toString
(int
radix of the String representation.
radix
)

Returns the String representation of this BigInteger in the given radix.

private static void
toString(BigInteger
The number to convert to a string.
u
,
StringBuilder
The StringBuilder that will be appended to in place.
sb
,
int
The base to convert to.
radix
,
int
The minimum number of digits to pad to.
digits
)

Converts the specified BigInteger to a string and appends to sb.

public String

Returns:

decimal String representation of this BigInteger.
toString
()

Overrides java.lang.Object.toString.

Returns the decimal String representation of this BigInteger.

private static int[]
trustedStripLeadingZeroInts(int[] val)

Returns the input array stripped of any leading zero ints.

pack-priv static long
unsignedLongPow(long x, int n)

Computes x^n using repeated squaring trick.

public static BigInteger

Returns:

a BigInteger with the specified value.
valueOf
(long
value of the BigInteger to return.
val
)

Returns a BigInteger whose value is equal to that of the specified long.

private static BigInteger
valueOf(int[] val)

Returns a BigInteger with the given two's complement representation.

private void
writeObject(ObjectOutputStream
the stream to serialize to.
s
)

Save the BigInteger instance to a stream.

public BigInteger

Returns:

this ^ val
xor
(BigInteger
value to be XOR'ed with this BigInteger.
val
)

Returns a BigInteger whose value is (this ^ val).

Inherited from java.lang.Number:
byteValueshortValue