BigInteger

class BigInteger : Comparable<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 kotlin.math. Additionally, BigInteger provides operations for modular arithmetic, GCD calculation, primality testing, prime generation, bit manipulation, and a few other miscellaneous operations.

Semantics of arithmetic operations exactly mimic those of Java's integer arithmetic operators, as defined in The Java Language Specification. For example, division by zero throws an ArithmeticException, and division of a negative by a positive yields a negative (or zero) remainder. All of the details in the Spec concerning overflow are ignored, as BigIntegers are made as large as necessary to accommodate the results of an operation.

Semantics of shift operations extend those of Java's shift operators to allow for negative shift distances. A right-shift with a negative shift distance results in a left shift, and vice-versa. The unsigned right shift operator (>>>) is omitted, as this operation makes little sense in combination with the "infinite word size" abstraction provided by this class.

Semantics of bitwise logical operations exactly mimic those of Java's bitwise integer operators. The binary operators (and, or, xor) implicitly perform sign extension on the shorter of the two operands prior to performing the operation.

Comparison operations perform signed integer comparisons, analogous to those performed by Java's relational and equality operators.

Modular arithmetic operations are provided to compute residues, perform exponentiation, and compute multiplicative inverses. These methods always return a non-negative result, between 0 and (modulus - 1), inclusive.

Bit operations operate on a single bit of the two's-complement representation of their operand. If necessary, the operand is sign- extended so that it contains the designated bit. None of the single-bit operations can produce a BigInteger with a different sign from the BigInteger being operated on, as they affect only a single bit, and the "infinite word size" abstraction provided by this class ensures that there are infinitely many "virtual sign bits" preceding each BigInteger.

For the sake of brevity and clarity, pseudo-code is used throughout the descriptions of BigInteger methods. The pseudo-code expression (i + j) is shorthand for "a BigInteger whose value is that of the BigInteger i plus that of the BigInteger j." The pseudo-code expression (i == j) is shorthand for "true if and only if the BigInteger i represents the same value as the BigInteger j." Other pseudo-code expressions are interpreted similarly.

All methods and constructors in this class throw NullPointerException when passed a null object reference for any input parameter.

BigInteger must support values in the range -2<sup>Int.MAX_VALUE</sup> (exclusive) to +2<sup>Int.MAX_VALUE</sup> (exclusive) and may support values outside of that 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 2<sup>500000000</sup>.

Author

Josh Bloch

Michael McCloskey

Alan Eliasen

Timothy Buktu

Since

1.1

See also

Constructors

BigInteger
Link copied to clipboard
common
fun BigInteger(val: ByteArray, off: Int = 0, len: Int = `val`.size)
Translates a byte sub-array containing the two's-complement binary representation of a BigInteger into a BigInteger.
BigInteger
Link copied to clipboard
common
fun BigInteger(signum: Int, magnitude: ByteArray, off: Int = 0, len: Int = magnitude.size)
Translates the sign-magnitude representation of a BigInteger into a BigInteger.
BigInteger
Link copied to clipboard
common
fun BigInteger(val: String, radix: Int = 10)
Translates the String representation of a BigInteger in the specified radix into a BigInteger.
BigInteger
Link copied to clipboard
common
fun BigInteger(numBits: Int, rnd: Random)
Constructs a randomly generated BigInteger, uniformly distributed over the range 0 to (2<sup>numBits</sup> - 1), inclusive.
BigInteger
Link copied to clipboard
common
fun BigInteger(bitLength: Int, certainty: Int, rnd: Random)
Constructs a randomly generated positive BigInteger that is probably prime, with the specified bitLength.

Types

Companion
Link copied to clipboard
common
object Companion

Functions

and
Link copied to clipboard
common
fun and(val: BigInteger): BigInteger
Returns a BigInteger whose value is (this & val).
andNot
Link copied to clipboard
common
fun andNot(val: BigInteger): BigInteger
Returns a BigInteger whose value is (this & ~val).
clearBit
Link copied to clipboard
common
fun clearBit(n: Int): BigInteger
Returns a BigInteger whose value is equivalent to this BigInteger with the designated bit cleared.
compareTo
Link copied to clipboard
common
open operator override fun compareTo(other: BigInteger): Int
Compares this BigInteger with the specified BigInteger.
div
Link copied to clipboard
common
operator fun div(val: BigInteger): BigInteger
Returns a BigInteger whose value is (this / val).
divideAndRemainder
Link copied to clipboard
common
fun divideAndRemainder(val: BigInteger): Array<BigInteger>
Returns an array of two BigIntegers containing (this / val) followed by (this % val).
equals
Link copied to clipboard
common
open operator override fun equals(other: Any?): Boolean
Compares this BigInteger with the specified Object for equality.
flipBit
Link copied to clipboard
common
fun flipBit(n: Int): BigInteger
Returns a BigInteger whose value is equivalent to this BigInteger with the designated bit flipped.
gcd
Link copied to clipboard
common
fun gcd(val: BigInteger): BigInteger
Returns a BigInteger whose value is the greatest common divisor of absoluteValue(this) and absoluteValue(val).
get
Link copied to clipboard
common
operator fun get(n: Int): Boolean
hashCode
Link copied to clipboard
common
open override fun hashCode(): Int
Returns the hash code for this BigInteger.
isProbablePrime
Link copied to clipboard
common
fun isProbablePrime(certainty: Int): Boolean
Returns true if this BigInteger is probably prime, false if it's definitely composite.
max
Link copied to clipboard
common
fun max(val: BigInteger): BigInteger
Returns the maximum of this BigInteger and val.
min
Link copied to clipboard
common
fun min(val: BigInteger): BigInteger
Returns the minimum of this BigInteger and val.
minus
Link copied to clipboard
common
operator fun minus(val: BigInteger): BigInteger
Returns a BigInteger whose value is (this - val).
modInverse
Link copied to clipboard
common
fun modInverse(m: BigInteger): BigInteger
Returns a BigInteger whose value is (this<sup>-1</sup> rem m).
modPow
Link copied to clipboard
common
fun modPow(exponent: BigInteger, m: BigInteger): BigInteger
Returns a BigInteger whose value is (this<sup>exponent</sup> rem m).
nextProbablePrime
Link copied to clipboard
common
fun nextProbablePrime(): BigInteger
Returns the first integer greater than this BigInteger that is probably prime.
not
Link copied to clipboard
common
operator fun not(): BigInteger
Returns a BigInteger whose value is (~this).
or
Link copied to clipboard
common
fun or(val: BigInteger): BigInteger
Returns a BigInteger whose value is (this | val).
plus
Link copied to clipboard
common
operator fun plus(val: BigInteger): BigInteger
Returns a BigInteger whose value is (this + val).
pow
Link copied to clipboard
common
infix fun pow(exponent: Int): BigInteger
Returns a BigInteger whose value is (this<sup>exponent</sup>).
rem
Link copied to clipboard
common
operator fun rem(m: BigInteger): BigInteger
Returns a BigInteger whose value is (this rem m).
remainder
Link copied to clipboard
common
fun remainder(val: BigInteger): BigInteger
Returns a BigInteger whose value is (this % val).
set
Link copied to clipboard
common
operator fun set(n: Int, b: Boolean)
setBit
Link copied to clipboard
common
fun setBit(n: Int): BigInteger
Returns a BigInteger whose value is equivalent to this BigInteger with the designated bit set.
shl
Link copied to clipboard
common
infix fun shl(n: Int): BigInteger
Returns a BigInteger whose value is (this << n).
shr
Link copied to clipboard
common
infix fun shr(n: Int): BigInteger
Returns a BigInteger whose value is (this >> n).
sqrt
Link copied to clipboard
common
fun sqrt(): BigInteger
Returns the integer square root of this BigInteger.
sqrtAndRemainder
Link copied to clipboard
common
fun sqrtAndRemainder(): Array<BigInteger>
Returns an array of two BigIntegers containing the integer square root s of this and its remainder this - s*s, respectively.
testBit
Link copied to clipboard
common
fun testBit(n: Int): Boolean
Returns true if and only if the designated bit is set.
times
Link copied to clipboard
common
operator fun times(val: BigInteger): BigInteger
Returns a BigInteger whose value is (this * val).
toByte
Link copied to clipboard
common
fun toByte(): Byte
toByteArray
Link copied to clipboard
common
fun toByteArray(): ByteArray
Returns a byte array containing the two's-complement representation of this BigInteger.
toByteExact
Link copied to clipboard
common
fun toByteExact(): Byte
Converts this BigInteger to a byte, checking for lost information.
toChar
Link copied to clipboard
common
fun toChar(): Char
toDouble
Link copied to clipboard
common
fun toDouble(): Double
toFloat
Link copied to clipboard
common
fun toFloat(): Float
toInt
Link copied to clipboard
common
fun toInt(): Int
Converts this BigInteger to an int.
toIntExact
Link copied to clipboard
common
fun toIntExact(): Int
Converts this BigInteger to an int, checking for lost information.
toLong
Link copied to clipboard
common
fun toLong(): Long
Converts this BigInteger to a long.
toLongExact
Link copied to clipboard
common
fun toLongExact(): Long
Converts this BigInteger to a long, checking for lost information.
toShort
Link copied to clipboard
common
fun toShort(): Short
toShortExact
Link copied to clipboard
common
fun toShortExact(): Short
Converts this BigInteger to a short, checking for lost information.
toString
Link copied to clipboard
common
open override fun toString(): String
Returns the decimal String representation of this BigInteger.
fun toString(radix: Int): String
Returns the String representation of this BigInteger in the given radix.
unaryMinus
Link copied to clipboard
common
operator fun unaryMinus(): BigInteger
Returns a BigInteger whose value is (-this).
unaryPlus
Link copied to clipboard
common
operator fun unaryPlus(): BigInteger
xor
Link copied to clipboard
common
fun xor(val: BigInteger): BigInteger
Returns a BigInteger whose value is (this ^ val).

Properties

absoluteValue
Link copied to clipboard
common
val absoluteValue: BigInteger
Returns a BigInteger whose value is the absolute value of this BigInteger.
bitCount
Link copied to clipboard
common
val bitCount: Int
Returns the number of bits in the two's complement representation of this BigInteger that differ from its sign bit.
bitLength
Link copied to clipboard
common
val bitLength: Int
Returns the number of bits in the minimal two's-complement representation of this BigInteger, excluding a sign bit.
signum
Link copied to clipboard
common
val signum: Int
Returns the _signum function of this BigInteger.

Extensions

toJava
Link copied to clipboard
fun BigInteger.toJava(): BigInteger