# Java.math.BigInteger Class

## Introduction

The java.math.BigInteger class provides operations analogues to all of Java’s primitive integer operators and for all relevant methods from java.lang.Math.

It also provides operations for modular arithmetic, GCD calculation, primality testing, prime generation, bit manipulation, and a few other miscellaneous operations. All operations behave as if BigIntegers were represented in two’s-complement notation.

Semantics of arithmetic operations and bitwise logical operations are similar to those of Java’s integer arithmetic operators and Java’s bitwise integer operators respectively. Semantics of shift operations extend those of Java’s shift operators to allow for negative shift distances.

Comparison operations perform signed integer comparisons. Modular arithmetic operations are provided to compute residues, perform exponentiation, and compute multiplicative inverses. Bit operations operate on a single bit of the two’s-complement representation of their operand.

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

## Class declaration

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

``````public class BigInteger
extends Number
implements Comparable<BigInteger>``````

## Field

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

• static BigInteger ONE — The BigInteger constant one.
• static BigInteger TEN — The BigInteger constant ten.
• static BigInteger ZERO — The BigInteger constant zero.

## Class constructors

S.N.Constructor & Description
1BigInteger(byte[] val)This constructor is used to translate a byte array containing the two’s-complement binary representation of a BigInteger into a BigInteger.
2BigInteger(int signum, byte[] magnitude)This constructor is used to translate the sign-magnitude representation of a BigInteger into a BigInteger.
3BigInteger(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.
4BigInteger(int numBits, Random rnd)This constructor is used to construct a randomly generated BigInteger, uniformly distributed over the range 0 to (2numBits – 1), inclusive.
5BigInteger(String val)This constructor is used to translate the decimal String representation of a BigInteger into a BigInteger.
6BigInteger(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
1BigInteger abs()

This method returns a BigInteger whose value is the absolute value of this BigInteger.

This method returns a BigInteger whose value is (this + val).

3BigInteger and(BigInteger val)

This method returns a BigInteger whose value is (this & val).

4BigInteger andNot(BigInteger val)

This method returns a BigInteger whose value is (this & ~val).

5int bitCount()

This method returns the number of bits in the two’s complement representation of this BigInteger that differ from its sign bit.

6int bitLength()

This method returns the number of bits in the minimal two’s-complement representation of this BigInteger, excluding a sign bit.

7BigInteger clearBit(int n)

This method returns a BigInteger whose value is equivalent to this BigInteger with the designated bit cleared.

8int compareTo(BigInteger val)

This method compares this BigInteger with the specified BigInteger.

9BigInteger divide(BigInteger val)

This method returns a BigInteger whose value is (this / val).

10BigInteger[ ] divideAndRemainder(BigInteger val)

This method returns an array of two BigIntegers containing (this / val) followed by (this % val).

11double doubleValue()

This method converts this BigInteger to a double.

12boolean equals(Object x)

This method compares this BigInteger with the specified Object for equality.

13BigInteger flipBit(int n)

This method returns a BigInteger whose value is equivalent to this BigInteger with the designated bit flipped.

14float floatValue()

This method converts this BigInteger to a float.

15BigInteger gcd(BigInteger val)

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

16int 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).
17int hashCode()

This method returns the hash code for this BigInteger.

18int intValue()

This method converts this BigInteger to an int.

19boolean isProbablePrime(int certainty)

This method returns true if this BigInteger is probably prime, false if it’s definitely composite.

20long longValue()

This method converts this BigInteger to a long.

21BigInteger max(BigInteger val)

This method returns the maximum of this BigInteger and val.

22BigInteger min(BigInteger val)

This method returns the minimum of this BigInteger and val.

23BigInteger mod(BigInteger m)

This method returns a BigInteger whose value is (this mod m).

24BigInteger modInverse(BigInteger m)

This method returns a BigInteger whose value is (this-1 mod m).

25BigInteger modPow(BigInteger exponent, BigInteger m)

This method returns a BigInteger whose value is (thisexponent mod m).

26BigInteger multiply(BigInteger val)

This method returns a BigInteger whose value is (this * val).

27BigInteger negate()

This method returns a BigInteger whose value is (-this).

28BigInteger nextProbablePrime()

This method returns the first integer greater than this BigInteger that is probably prime.

29BigInteger not()

This method returns a BigInteger whose value is (~this).

30BigInteger or(BigInteger val)

This method returns a BigInteger whose value is (this | val).

31BigInteger pow(int exponent)

This method returns a BigInteger whose value is (thisexponent).

32static BigInteger probablePrime(int bitLength, Random rnd)

This method returns a positive BigInteger that is probably prime, with the specified bitLength.

33BigInteger remainder(BigInteger val)

This method returns a BigInteger whose value is (this % val).

34BigInteger setBit(int n)

This method returns a BigInteger whose value is equivalent to this BigInteger with the designated bit set.

35BigInteger shiftLeft(int n)

This method returns a BigInteger whose value is (this << n).

36BigInteger shiftRight(int n)

This method returns a BigInteger whose value is (this >> n).

37int signum()

This method returns the signum function of this BigInteger.

38BigInteger subtract(BigInteger val)

This method returns a BigInteger whose value is (this – val).

39boolean testBit(int n)

This method returns true if and only if the designated bit is set.

40byte[ ] toByteArray()

This method returns a byte array containing the two’s-complement representation of this BigInteger.

41String toString()

This method returns the decimal String representation of this BigInteger.

This method returns the String representation of this BigInteger in the given radix.

43static BigInteger valueOf(long val)

This method returns a BigInteger whose value is equal to that of the specified long.

44BigInteger xor(BigInteger val)

This method returns a BigInteger whose value is (this ^ val).