Contents

## BigInteger Class In Java

BigInteger supports arbitrary precision integer value. This means that we can accurately represent integral values of any size without losing any information during operation.

BigInteger is not limited to 64 bits (like long). This class defines some methods that do the same thing as normal arithmetic operator or bit manipulation operators does.

We should use this class if a long is not big enough for your purposes. The number in a BigInteger is represented by a sign value and a magnitude, which is an arbitrarily large array of bytes. A BigInteger cannot overflow.

Most of the methods in BigInteger perform mathematical operations or make comparisons with other BigInteger objects. BigInteger also defines some methods for handling modular arithmetic and determining primality that is needed for cryptographic purposes.

### The structure of the BigInteger class defined as:

```
public class java.math.BigInteger extends java.lang.Number {
//Constructors
public BigInteger(byte[] val) throws NumberFormatException;
public BigInteger(int signum, byte[] magnitude) throws NumberFormatException;
public BigInteger(String val, int radix) throws NumberFormatException;
public BigInteger(String val) throws NumberFormatException;
public BigInteger(int numBits, Random rndSrc) throws IllegalArgumentException;
public BigInteger(int bitLength, int certainty, Random rnd);
//Methods
public static BigInteger valueOf(long val);
public BigInteger abs();
public BigInteger add(BigInteger val) throws ArithmeticException;
public BigInteger and(BigInteger val);
public BigInteger andNot(BigInteger val);
public int bitCount();
public int bitLength();
public BigInteger clearBit(int n) throws ArithmeticException;
public int compareTo(BigInteger val);
public BigInteger divide(BigInteger val) throws ArithmeticException;
public BigInteger[] divideAndRemainder(BigInteger val) throws ArithmeticException;
public double doubleValue();
public boolean equals(Object x);
public BigInteger flipBit(int n) throws ArithmeticException;
public float floatValue();
public BigInteger gcd(BigInteger val);
public int getLowestSetBit();
public int hashCode();
public int intValue();
public boolean isProbablePrime(int certainty);
public long longValue();
public BigInteger max(BigInteger val);
public BigInteger min(BigInteger val);
public BigInteger mod(BigInteger m);
public BigInteger modInverse(BigInteger m) throws ArithmeticException;
public BigInteger modPow(BigInteger exponent, BigInteger m);
public BigInteger multiply(BigInteger val);
public BigInteger negate();
public BigInteger not();
public BigInteger or(BigInteger val);
public BigInteger pow(int exponent) throws ArithmeticException;
public BigInteger remainder(BigInteger val) throws ArithmeticException;
public BigInteger setBit(int n) throws ArithmeticException;
public BigInteger shiftLeft(int n);
public BigInteger shiftRight(int n);
public int signum();
public BigInteger subtract(BigInteger val);
public boolean testBit(int n) throws ArithmeticException;
public byte[] toByteArray();
public String toString(int radix);
public String toString();
public BigInteger xor(BigInteger val);
}
```

The details of the class structure are given as follows:

`public BigInteger(byte[] val);`

public BigInteger(byte[] val) constructor creates a BigInteger with the given initial value. The value is expressed as a two’s complement signed integer, with the most significant byte in the first position (val[0]) of the array (big-endian). The most significant bit of the most significant byte is the sign bit.

**Parameter**

**val** – The initial value.

`public BigInteger(int signum, byte[] magnitude);`

public BigInteger(int signum, byte[] magnitude) constructor creates a BigInteger with the given initial value and sign. The magnitude is expressed as a big-endian byte array.

**Parameter**

**signum** – The sign of the value: -1 indicates negative, 0 indicates zero, and 1 indicates positive.

**magnitude** – The initial magnitude of the value.

`public BigInteger(String val);`

public BigInteger(String val) constructor creates a BigInteger with the initial value specified by the String. The string can contain an optional minus sign followed by zero or more decimal digits. The mapping from characters to digits is provided by the Character.digit() method.

**Parameter**

**val** – The initial value.

`public BigInteger(String val, int radix);`

public BigInteger(String val, int radix) constructor creates a BigInteger with the initial value specified by the String using the given radix. The string can contain an optional minus sign followed by zero or more digits in the specified radix. The mapping from characters to digits is provided by the Character.digit() method.

**Parameter**

**val** – The initial value.

**radix** – The radix to use to parse the given string.

`public BigInteger(int numBits, Random rndSrc);`

public BigInteger(int numBits, Random rndSrc) constructor creates a random BigInteger in the range 0 to 2^numBits -1.

**Parameter**

**numBits** – The maximum number of bits in the returned number.

**rndSrc** – The source of the random bits.

`public BigInteger(int bitLength, int certainty, Random rnd);`

public BigInteger(int bitLength, int certainty, Random rnd) constructor creates a random BigInteger in the range 0 to 2^numBits-1 that is probably a prime number. The probability that the returned number is prime is greater than 1-.5^certainty. In other words, the higher the value of certainty, the more likely the BigInteger is to be prime, and also the longer it takes for the constructor to create the BigInteger.

**Parameter**

**bitLength** – The maximum number of bits in the returned number.

**certainty** – The certainty that the returned value is a prime number.

**rnd** – The source of the random bits.

`public static BigInteger valueOf(long val);`

public static BigInteger valueOf(long val) method creates a BigInteger from the given long value.

This method returns a BigInteger that represents the given value.

**Parameter**

**val** – The initial value.

`public BigInteger abs();`

public BigInteger abs() method returns the absolute value of this BigInteger. If this BigInteger is nonnegative, it is returned. Otherwise, a new BigInteger that contains the absolute value of this BigInteger is returned.

This method returns a BigInteger that contains the absolute value of this number.

`public BigInteger add(BigInteger val);`

public BigInteger add(BigInteger val) method returns the sum of this BigInteger and the given BigInteger as a new BigInteger.

This method returns a new BigInteger that contains the sum of this number and the given value.

**Parameter**

**val** – The number to be added.

`public BigInteger and(BigInteger val);`

public BigInteger and(BigInteger val) method returns the bitwise AND of this BigInteger and the supplied BigInteger as a new BigInteger.

This method returns a new BigInteger that contains the bitwise AND of this number and the given value.

**Parameter**

**val** – The number to be ANDed.

`public BigInteger andNot(BigInteger val);`

public BigInteger andNot(BigInteger val) method returns the bitwise AND of this BigInteger and the bitwise negation of the given BigInteger as a new BigInteger. Calling this method is equivalent to calling and(val.not()).

This method returns a new BigInteger that contains the bitwise AND of this number and the bitwise negation of the given value.

**Parameter**

**val** – The number to be combined with this BigInteger.

`public int bitCount();`

public int bitCount() method returns the number of bits in the two’s complement representation of this BigInteger that differ from the sign bit of this BigInteger.

This method returns the number of bits that differ from this BigInteger’s sign bit.

`public int bitLength();`

public int bitLength() method returns the minimum number of bits needed to represent this number, not counting a sign bit.

This method returns the number of bits needed to represent this number, excluding a sign bit.

`public BigInteger clearBit(int n);`

public BigInteger clearBit(int n) method returns a new BigInteger that is equal to this BigInteger, except that the given bit is cleared, or set to zero.

This method returns a new BigInteger that contains the value of this BigInteger with the given bit cleared.

**Parameter**

**n** – The bit to clear.

`public int compareTo(BigInteger val);`

public int compareTo(BigInteger val) method compares this BigInteger to the given BigInteger and returns a value that indicates the result of the comparison. This method can be used to implement all six of the standard boolean comparison operators: ==, !=, <=, <, >=, and >.

This method returns -1 if this number is less than val, 0 if this number is equal to val, or 1 if this number is greater than val.

**Parameter**

**val** – The value to be compared.

`public BigInteger divide(BigInteger val);`

public BigInteger divide(BigInteger val) method returns the quotient that results from dividing this BigInteger by the given BigInteger as a new BigInteger. Any fractional remainder is discarded.

This method returns a new BigInteger that contains the result (quotient) of dividing this number by the given value.

**Parameter**

**val** – The divisor.

`public BigInteger[] divideAndRemainder(BigInteger val);`

public BigInteger[] divideAndRemainder(BigInteger val) method returns the quotient and remainder that results from dividing this BigInteger by the given BigInteger as an array of new BigInteger objects. The first element of the array is equal to divide(val);the second element is equal to remainder(val).

This method returns an array of BigInteger objects that contains the quotient and remainder (in that order) that result from dividing this number by the given value.

**Parameter**

**val** – The divisor.

`public double doubleValue();`

public double doubleValue() method returns the value of this BigInteger as a double. If the value exceeds the limits of a double, Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY is returned.

This method returns the value of this BigInteger as a double.

`public boolean equals(Object x);`

public boolean equals(Object x) method returns true if x is an instance of BigInteger, and it represents the same value as this BigInteger.

This method returns true if the objects are equal; false if they are not.

**Parameter**

**x** – The object to be compared with this object.

`public BigInteger flipBit(int n);`

public BigInteger flipBit(int n) method returns a new BigInteger that is equal to this BigInteger, except that the given bit is toggled. In other words, if the given bit is 0, it is set to one, or if it is 1, it is set to zero.

This method returns a new BigInteger that contains the value of this BigInteger with the given bit toggled.

**Parameter**

**n** – The bit to toggle.

`public float floatValue();`

public float floatValue() method returns the value of this BigInteger as a float. If the value exceeds the limits of a float, Float.POSITIVE_INFINITY or Float.NEGATIVE_INFINITY is returned.

This method returns the value of this BigInteger as a float.

`public BigInteger gcd(BigInteger val);`

public BigInteger gcd(BigInteger val) method calculates the greatest common denominator of the absolute value of this BigInteger and the absolute value of the given BigInteger, and returns it as a new BigInteger. If both values are 0, the method returns a BigInteger that contains the value 0.

This method returns a new BigInteger that contains the greatest common denominator of this number and the given number.

**Parameter**

**val** – The number to be compared.

`public int getLowestSetBit();`

public int getLowestSetBit() method returns the index of the lowest-order, or rightmost, bit with a value of 1.

This method returns the index of the lowest-order bit with a value of 1, or -1 if there are no bits that are 1.

`public int hashCode();`

public int hashCode() method returns a hashcode for this BigInteger.

This method returns a hashcode for this object.

`public int intValue();`

public int intValue() method returns the value of this BigInteger as an int. If the value exceeds the limits of an int, the excessive high-order bits are discarded.

This method returns the value of this BigInteger as an int.

`public boolean isProbablePrime(int certainty);`

public boolean isProbablePrime(int certainty) method returns true if this number has a probability of being prime that is greater than 1-.5^certainty. If the number is definitely not prime, false is returned.

This method returns true if this number is probably prime; false if it is definitely not prime.

**Parameter**

**certainty** – The “certainty” that this number is prime, where a higher value indicates more certainty.

`public long longValue();`

public long longValue() method returns the value of this BigInteger as a long. If the value exceeds the limits of a long, the excessive high-order bits are discarded.

This method returns the value of this BigInteger as a long

`public BigInteger max(BigInteger val);`

public BigInteger max(BigInteger val) method returns the greater of this BigInteger and the given BigInteger.

This method returns the BigInteger that represents the greater of this number and the given value.

**Parameter**

**val** – The number to be compared.

`public BigInteger min(BigInteger val);`

public BigInteger min(BigInteger val) method returns the lesser of this BigInteger and the given BigInteger.

This method returns the BigInteger that represents the lesser of this number and the given value.

**Parameter**

**val** – The number to be compared.

`public BigInteger mod(BigInteger m);`

public BigInteger mod(BigInteger m) method returns a new BigInteger that contains the value of this BigInteger mod m.

This method returns a new BigInteger that contains the modulus of this number and the given number.

**Parameter**

**m** – The number to use.

`public BigInteger modInverse(BigInteger m);`

public BigInteger modInverse(BigInteger m) method returns a new BigInteger that contains the multiplicative inverse of the value of this BigInteger mod m.

This method returns a new BigInteger that contains the multiplicative inverse of the modulus of this number and the given number.

**Parameter**

**m** – The number to use.

`public BigInteger modPow(BigInteger exponent, BigInteger m);`

public BigInteger modPow(BigInteger exponent, BigInteger m) method returns a new BigInteger that contains the value of this BigInteger raised to the given power mod m.

This method returns a new BigInteger that contains the modulus of this number raised to the given power and the given number.

**Parameter**

**exponent** – The exponent.

**m** – The number to use.

`public BigInteger multiply(BigInteger val);`

public BigInteger multiply(BigInteger val) method multiplies this BigInteger by the given BigInteger and returns the product as a new BigInteger.

This method returns a new BigInteger that contains the product of this number and the given number.

**Parameter**

**val** – The number to be multiplied.

`public BigInteger negate();`

public BigInteger negate() method returns a new BigInteger that is identical to this BigInteger except that its sign is reversed.

This method returns a new BigInteger that contains the negative of this number.

`public BigInteger not();`

public BigInteger not() method returns a new BigInteger that is calculated by inverting every bit of this BigInteger.

This method returns a new BigInteger that contains the bitwise negation of this number.

`public BigInteger or(BigInteger val);`

public BigInteger or(BigInteger val) method returns the bitwise OR of this BigInteger and the given BigInteger as a new BigInteger.

This method returns a new BigInteger that contains the bitwise OR of this number and the given value.

**Parameter**

**val** – The value to be ORed.

`public BigInteger pow(int exponent);`

public BigInteger pow(int exponent) method raises this BigInteger to the given power and returns the result as a new BigInteger.

This method returns a new BigInteger that contains the result of raising this number to the given power.

**Parameter**

**exponent** – The exponent.

`public BigInteger remainder(BigInteger val);`

public BigInteger remainder(BigInteger val) method returns the remainder that results from dividing this BigInteger by the given BigInteger as a new BigInteger.

This method returns a new BigInteger that contains the remainder that results from dividing this number by the given value.

**Parameter**

**val** – The divisor.

`public BigInteger setBit(int n);`

public BigInteger setBit(int n) method returns a new BigInteger that is equal to this BigInteger, except that the given bit is set to 1.

This method returns a new BigInteger that contains the value of this BigInteger with the given bit set.

**Parameter**

**n** – The bit to set.

`public BigInteger shiftLeft(int n);`

public BigInteger shiftLeft(int n) method returns a new BigInteger that contains the value of this BigInteger left-shifted by the given number of bits.

This method returns a new BigInteger that contains the result of shifting the bits of this number left by the given number of bits.

**Parameter**

**n** – The number of bits to shift.

`public BigInteger shiftRight(int n);`

public BigInteger shiftRight(int n) method returns a new BigInteger that contains the value of this BigInteger right-shifted by the given number of bits with sign extension.

This method returns a new BigInteger that contains the result of shifting the bits of this number right by the given number of bits with sign extension.

**Parameter**

**n** – The number of bits to shift.

`public int signum();`

public int signum() method returns a value that indicates the sign of this BigInteger.

This method returns -1 is this number is negative, 0 if this number is zero, or 1 if this number is positive.

`public BigInteger subtract(BigInteger val);`

public BigInteger subtract(BigInteger val) method subtracts the given BigInteger from this BigInteger and returns the result as a new BigInteger.

This method returns a new BigDecimal that contains the result of subtracting the given number from this number.

**Parameter**

**val** – The number to be subtracted.

`public boolean testBit(int n);`

public boolean testBit(int n) method returns true if the specified bit in this BigInteger is 1. Otherwise the method returns false.

This method returns true if the specified bit is 1; false if the specified bit is 0.

**Parameter**

**n** – The bit to test.

`public byte[] toByteArray();`

public byte[] toByteArray() method returns an array of bytes that contains the two’s complement representation of this BigInteger. The most significant byte is in the first position (val[0]) of the array. The array can be used with the BigInteger(byte[]) constructor to reconstruct the number.

This method returns an array of bytes that represents this object.

`public String toString();`

public String toString() method returns a string representation of this BigInteger in decimal form. A minus sign represents the sign if necessary. The mapping from digits to characters is provided by the Character.forDigit() method.

This method returns a string representation of this object in decimal form.

`public String toString(int radix);`

public String toString(int radix) method returns a string representation of this BigInteger for the given radix. A minus sign is used to represent the sign if necessary. The mapping from digits to characters is provided by the

Character.forDigit() method.

This method returns a string representation of this object in the given radix.

**Parameter**

**radix** – The radix to use.

`public BigInteger xor(BigInteger val);`

public BigInteger xor(BigInteger val) method returns the bitwise XOR of this BigInteger and the given BigInteger as a new BigInteger.

This method returns a new BigInteger that contains the bitwise XOR of this number and the given value.

**Parameter**

**val** – The value to be XORed.

Apart from these BigInteger class also has inherited methods from class- Object. They are as follows:

- clone()
- finalize()
- hashCode()
- notifyAll()
- wait()
- wait(long, int)
- equals(Object)
- getClass()
- notify()
- toString()
- wait(long)