java.Math BigInteger
The BigInteger is an 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. All the good stuff in dealing with Mathematical operations are available in BigInteger class.
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.
In dealing with BigInteger, usual mathematical operators such as + – * / are not applicable, instead methods such as add,subtract,multiply and divide are available to server this purpose.
BigInteger Class Syntax
public class BigInteger
extends Number
implements Comparable<BigInteger>
Java BigInteger Compatibility
The BigInteger class has been around since Java 1.1 though some of it’s methods were added on the later releases.
BigInteger Method Usage Examples
The following are the detailed list of BigInteger methods and descriptions. We have also provided links to examples of each method on the list.
Modifier and Type | Method and Description |
---|---|
BigInteger | abs() Basically this method returns the absolute value of this BigInteger object. This is very useful if we are dealing only with unsigned integer. |
BigInteger | add(BigInteger val) This method performs addition of this BigInteger and the method argument. The BigInteger doesn’t conform with the normal mathematical operation such as +, -, /,*. Instead of using the mathematical operators, methods such as add() were exposed to provide facility of mathematical operations. |
BigInteger | and(BigInteger val) Returns a BigInteger whose value is (this & val). (This method returns a negative BigInteger if and only if this and val are both negative.) |
BigInteger | andNot(BigInteger val) Returns a BigInteger whose value is (this & ~val). This method, which is equivalent to and(val.not()), is provided as a convenience for masking operations. (This method returns a negative BigInteger if and only if this is negative and val is positive.) |
int | bitCount() Returns the number of bits in the two’s complement representation of this BigInteger that differ from its sign bit. This method is useful when implementing bit-vector style sets atop BigIntegers. |
int | bitLength() Returns the number of bits in the minimal two’s-complement representation of this BigInteger, excluding a sign bit. For positive BigIntegers, this is equivalent to the number of bits in the ordinary binary representation. |
byte | byteValueExact() Converts this 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 | clearBit(int n) Returns a BigInteger whose value is equivalent to this BigInteger with the designated bit cleared. |
int | compareTo(BigInteger val) Compares this BigInteger with the specified BigInteger. |
BigInteger | divide(BigInteger val) Returns a BigInteger whose value is (this / val). |
BigInteger[] | divideAndRemainder(BigInteger val) Returns an array of two BigIntegers containing (this / val) followed by (this % val). |
double | doubleValue() Converts this BigInteger to a double. |
boolean | equals(Object x) Compares this BigInteger with the specified Object for equality. |
BigInteger | flipBit(int n) Returns a BigInteger whose value is equivalent to this BigInteger with the designated bit flipped. |
float | floatValue() Converts this BigInteger to a float. |
BigInteger | gcd(BigInteger val) Returns a BigInteger whose value is the greatest common divisor of abs(this) and abs(val). |
int | getLowestSetBit() Returns the index of the rightmost (lowest-order) one bit in this BigInteger (the number of zero bits to the right of the rightmost one bit). |
int | hashCode() Returns the hash code for this BigInteger. |
int | intValue() Converts this BigInteger to an int. |
int | intValueExact() Converts this BigInteger to an int, checking for lost information. |
boolean | isProbablePrime(int certainty) Returns true if this BigInteger is probably prime, false if it’s definitely composite. |
long | longValue() Converts this BigInteger to a long. |
long | longValueExact() Converts this BigInteger to a long, checking for lost information. |
BigInteger | max(BigInteger val) Returns the maximum of this BigInteger and val. |
BigInteger | min(BigInteger val) Returns the minimum of this BigInteger and val. |
BigInteger | mod(BigInteger m) Returns a BigInteger whose value is (this mod m). |
BigInteger | modInverse(BigInteger m) Returns a BigInteger whose value is (this-1 mod m). |
BigInteger | modPow(BigInteger exponent, BigInteger m) Returns a BigInteger whose value is (thisexponent mod m). |
BigInteger | multiply(BigInteger val) Returns a BigInteger whose value is (this * val). |
BigInteger | negate() Returns a BigInteger whose value is (-this). |
BigInteger | nextProbablePrime() Returns the first integer greater than this BigInteger that is probably prime. |
BigInteger | not() Returns a BigInteger whose value is (~this). |
BigInteger | or(BigInteger val) Returns a BigInteger whose value is (this | val). |
BigInteger | pow(int exponent) Returns a BigInteger whose value is (thisexponent). |
static BigInteger | probablePrime(int bitLength, Random rnd) Returns a positive BigInteger that is probably prime, with the specified bitLength. |
BigInteger | remainder(BigInteger val) Returns a BigInteger whose value is (this % val). |
BigInteger | setBit(int n) Returns a BigInteger whose value is equivalent to this BigInteger with the designated bit set. |
BigInteger | shiftLeft(int n) Returns a BigInteger whose value is (this << n). |
BigInteger | shiftRight(int n) Returns a BigInteger whose value is (this >> n). |
short | shortValueExact() Converts this BigInteger to a short, checking for lost information. |
int | signum() Returns the signum function of this BigInteger. |
BigInteger | subtract(BigInteger val) Returns a BigInteger whose value is (this – val). |
boolean | testBit(int n) Returns true if and only if the designated bit is set. |
byte[] | toByteArray() Returns a byte array containing the two’s-complement representation of this BigInteger. |
String | toString() Returns the decimal String representation of this BigInteger. |
String | toString(int radix) Returns the String representation of this BigInteger in the given radix. |
static BigInteger | valueOf(long val) Returns a BigInteger whose value is equal to that of the specified long. |
BigInteger | xor(BigInteger val) Returns a BigInteger whose value is (this ^ val). |