# Java.math.BigInteger Class - Java.math Package

« Previous Topics
Java.math.bigdecimal Class

## Introduction

The java.math.BigInteger class presents operations analogues to all of Java's primitive integer operators and for all relevant techniques from java.lang.Math.

It also presents operations for modular arithmetic, GCD calculation, primality testing, prime generation, bit manipulation, and a few other miscellaneous operations. All operations behave as if BigIntegers have been represented in 's-supplement notation.

Semantics of arithmetic operations and bitwise logical operations are just like the ones of Java's integer arithmetic operators and Java's bitwise integer operators respectively. Semantics of shift operations increase those of Java's shift operators to allow for negative shift distances.

comparison operations perform signed integer comparisons. Modular mathematics operations are supplied to compute residues, carry out exponentiation, and compute multiplicative inverses. Bit operations function on a single bit of the two's-supplement representation in their operand.

All strategies and constructors on this class throw NullPointerException when passed a null object reference for any enter parameter.

## Class Declaration

Following is the declaration for java.math.BigInteger class –

## Field

Following are the fields for java.math.BigInteger class −

• Static Big Integer ONE − The BigInteger constant one.
• Static Big Integer TEN − The BigInteger constant ten.
• Static Big Integer ZERO − The BigInteger constant zero.

## Class Constructors

 S.N. Constructor & Description 1 BigInteger(byte[] val) This constructor is used to translate a byte array containing the two's-complement binary representation of a BigInteger into a BigInteger. 2 BigInteger(int signum, byte[] magnitude) This constructor is used to translate the sign-magnitude representation of a BigInteger into a BigInteger. 3 BigInteger(int bitLength, int certainty, Random rnd) This constructor is used to construct a randomly generated positive BigInteger that is probably prime, with the specified bitLength. 4 BigInteger(int numBits, Random rnd) This constructor is used to construct a randomly generated BigInteger, uniformly distributed over the range 0 to (2numBits- 1), inclusive. 5 BigInteger(String val) This constructor is used to translate the decimal String representation of a BigInteger into a BigInteger. 6 BigInteger(String val, int radix) This constructor is used to translate the String representation of a BigInteger in the specified radix into a BigInteger.

## Class methods

 S.N. Method & Description 1 Big Integer abs() This method returns a BigInteger whose value is the absolute value of this BigInteger. 2 Big Integer add(BigInteger val) This method returns a BigInteger whose value is (this + val). 3 Big Integer and(BigInteger val) This method returns a BigInteger whose value is (this & val). 4 Big Integer andNot(BigInteger val) This method returns a BigInteger whose value is (this & ~val). 5 int bitCount() This method returns the number of bits in the two's complement representation of this BigInteger that differ from its sign bit. 6 int bitLength() This method returns the number of bits in the minimal two's-complement representation of this BigInteger, excluding a sign bit. 7 Big Integer clearBit(int n) This method returns a BigInteger whose value is equivalent to this BigInteger with the designated bit cleared. 8 int compareTo(BigInteger val) This method compares this BigInteger with the specified BigInteger. 9 Big Integer divide(BigInteger val) This method returns a BigInteger whose value is (this / val). 10 Big Integer[ ] divideAndRemainder(BigInteger val) This method returns an array of two BigIntegers containing (this / val) followed by (this % val). 11 double doubleValue() This method converts this BigInteger to a double. 12 Boolean equals(Object x) This method compares this BigInteger with the specified Object for equality. 13 BigInteger flipBit(int n) This method returns a BigInteger whose value is equivalent to this BigInteger with the designated bit flipped. 14 float floatValue() This method converts this BigInteger to a float. 15 Big Integer gcd(BigInteger val) This method returns a BigInteger whose value is the greatest common divisor of abs(this) and abs(val). 16 int getLowestSetBit() This method 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). 17 int hashCode() This method returns the hash code for this BigInteger. 18 int intValue() This method converts this BigInteger to an int. 19 Boolean isProbablePrime(int certainty) This method returns true if this BigInteger is probably prime, false if it's definitely composite. 20 long longValue() This method converts this BigInteger to a long. 21 Big Integer max(BigInteger val) This method returns the maximum of this BigInteger and val. 22 Big Integer min(BigInteger val) This method returns the minimum of this BigInteger and val. 23 Big Integer mod(BigInteger m) This method returns a BigInteger whose value is (this mod m). 24 Big Integer modInverse(BigInteger m) This method returns a BigInteger whose value is (this-1mod m). 25 Big Integer modPow(BigInteger exponent, BigInteger m) This method returns a BigInteger whose value is (thisexponentmod m). 26 Big Integer multiply(BigInteger val) This method returns a BigInteger whose value is (this * val). 27 Big Integer negate() This method returns a BigInteger whose value is (-this). 28 Big Integer nextProbablePrime() This method returns the first integer greater than this BigInteger that is probably prime. 29 Big Integer not() This method returns a BigInteger whose value is (~this). 30 Big Integer or(BigInteger val) This method returns a BigInteger whose value is (this | val). 31 Big Integer pow(int exponent) This method returns a BigInteger whose value is (thisexponent). 32 static BigInteger probablePrime(int bitLength, Random rnd) This method returns a positive BigInteger that is probably prime, with the specified bitLength. 33 BigInteger remainder(BigInteger val) This method returns a BigInteger whose value is (this % val). 34 Big Integer setBit(int n) This method returns a BigInteger whose value is equivalent to this BigInteger with the designated bit set. 35 Big Integer shiftLeft(int n) This method returns a BigInteger whose value is (this << n). 36 Big Integer shiftRight(int n) This method returns a BigInteger whose value is (this >> n). 37 int signum() This method returns the signum function of this BigInteger. 38 Big Integer subtract(BigInteger val) This method returns a BigInteger whose value is (this - val). 39 Boolean testBit(int n) This method returns true if and only if the designated bit is set. 40 byte[ ] toByteArray() This method returns a byte array containing the two's-complement representation of this BigInteger. 41 String toString() This method returns the decimal String representation of this BigInteger. 42 String toString(int radix) This method returns the String representation of this BigInteger in the given radix. 43 static BigInteger valueOf(long val) This method returns a BigInteger whose value is equal to that of the specified long. 44 Big Integer xor(BigInteger val) This method returns a BigInteger whose value is (this ^ val).

« Previous Topics
Java.math.bigdecimal Class