Java BigInteger

This section will describe you about how to work with the BigInteger in Java.

Java BigInteger

This section will describe you about how to work with the BigInteger in Java.

Java BigInteger

Java BigInteger

In this section we will learn about the BigInteger in Java. We will read here the various constructors of BigInteger, methods of BigInteger, and simple example of BigInteger.

BigInteger is a immutable class i.e. which object once created can't be change. BigInteger should be used where you need to deal with larger numbers. BigInteger can be used in Security to provide the keys specifications. You can perform all the operations that you perform on two primitive integer types in Java. In addition to BigInteger includes methods for the operations for modular arithmetic such as GCD calculation, primality testing, prime generation, etc. Arithmetic operations on BigInteger behaves exactly like the arithmetic operations on Java integers such as if you divide a biginteger number by zero then it throws the ArithmeticException, if a negative number is divide by a positive number then it gives the negative remainder or zero.

Class Declaration

public class BigInteger extends Number
implements Comparable

Constructors Of BigInteger

Constructor Detail
BigInteger(byte[] val) This constructor is used to create a BigInteger from the specified byte array.
BigInteger(int signum, byte[] magnitude) This constructor is used to create a BigInteger from the given sign magnitude.
BigInteger(int bitLength, int certainty, Random rnd) This constructor is used to create a BigInteger from the specified randomly generated positive number of the specified bitLength which is probably prime.
BigInteger(int numBits, Random rnd) This constructor is used to create a BigInteger from the specified randomly generated number in the range from 0 to (2numBits -1).
BigInteger(String val) This constructor is used to create a BigInteger from the String representation of a decimal value.
BigInteger(String val, int radix) This constructor is used to create a BigInteger of a String representation in the specified radix.

Methods Of BigInteger

Method Name Method Detail
BigInteger abs() This method is used to find the absolute value of the current BigInteger.
Synatx : public BigInteger abs()
 
BigInteger add(BigInteger val) This method is used to add the current BigInteger value with the given BigInteger value.
Syntax : public BigInteger add(BigInteger val)
 
BigInteger and(BigInteger val) This method is used to find the bitwise and value with the current BigInteger value and the given BigInteger value.
Syntax : public BigInteger and(BigInteger val)
 
BigInteger andNot(BigInteger val) This method is used to find the BigInteger value as negation of and(BigInteger val).
Syntax : public BigInteger andNot(BigInteger val)
 
int bitCount() This method is used to find out the number of bits of the current BigInteger.
Syntax : public int bitCount()
 
int bitLength() This method is used to find out the bitLength of the current BigInteger excluding the sign bit.
Syntax : public int bitLength()
 
BigInteger clearBit(int n) This method is used to clear the bit of the specified index of the current BigInteger.
Syntax : public BigInteger clearBit(int n)
 
int compareTo(BigInteger val) This method is used to compare the current BigInteger with the given BigInteger.
Syntax : public int compareTo(BigInteger val)
 
BigInteger divide(BigInteger val) This method is used to find the quotient after dividing the current BigInteger by the specified BigInteger value.
Syntax : public BigInteger divide(BigInteger val)
 
BigInteger[] divideAndRemainder(BigInteger val) This method is used to find out the array of two values containing the quotient after calculating current BigInteger/given BigInteger and remainder after current BigInteger % given BigInteger value.
Syntax : public BigInteger[] divideAndRemainder(BigInteger val)
 
double doubleValue() This method is used to convert a BigInteger value to a double.
Syntax : public double doubleValue()
 
boolean equals(Object x) This method is used to check out the equality of the current BigInteger and the given Object.
Syntax : public boolean equals(Object x)
 
BigInteger flipBit(int n) This method is used to find out the BigInteger value which value is equivalent to the current BigInteger with the given bit flipped.
Syntax : public BigInteger flipBit(int n)
 
float floatValue() This method is used to convert a BigInteger value to the float.
Syntax : public float floatValue()
 
BigInteger gcd(BigInteger val) This method is used to find out the greatest common divisor value of the current BigInteger and the given BigInteger value.
Syntax : public BigInteger gcd(BigInteger val)
 
int getLowestSetBit() This method is used to find out the index of the right most one bit of the current BigInteger.
Syntax : public int getLowestSetBit()
 
int hashCode() This method is used to find out the hash code of the current BigInteger.
Syntax : public int hashCode()
 
int intValue() This method is used to convert the current BigInteger value into an int.
Syntax : public int intValue()
 
boolean isProbablePrime(int certainty) This method is used to check whether the current BigInteger is probably prime or not. Returns false if the BigInteger is composite.
Syntax : public boolean isProbablePrime(int certainty)
 
long longValue() This method is used to convert the current BigInteger value in long.
Syntax : public long longValue()
 
BigInteger max(BigInteger val) This method is used to find out the maximum value from the current BigInteger value and the given BigInteger value.
Syntax : public BigInteger max(BigInteger val)
 
BigInteger min(BigInteger val) This method is used to find out the minimum value from the current BigInteger value and the given BigInteger value.
Syntax : public BigInteger min(BigInteger val)
 
BigInteger mod(BigInteger m) This method is used to find out the modulus of the current BigInteger and the given BigInteger. Calculated as (this mod m).
Syntax : public BigInteger mod(BigInteger m)
 
BigInteger modInverse(BigInteger m) This method is used to find out the modulus of the inverse current BigInteger and the given BigInteger. Caluculated as (this-1 mod m).
Syntax : public BigInteger modInverse(BigInteger m)
 
BigInteger modPow(BigInteger exponent, BigInteger m) This method is used to find out the modulus of the exponent of the current BigInteger and given BigInteger. Calculated as (thisexp mod m).
Syntax : public BigInteger modPow(BigInteger exponent, BigInteger m)
 
BigInteger multiply(BigInteger val) This method is used to multiply the current BigInteger and the given BigInteger value.
Syntax : public BigInteger multiply(BigInteger val)
 
BigInteger negate() This method is used to find out the negative number of the current BigInteger value.
Syntax : public BigInteger negate()
 
BigInteger nextProbablePrime() This method is used to find out the first integer greater than the current BigInteger that is probably prime.
Syntax : public BigInteger nextProbablePrime()
 
BigInteger not() This method is used to find out the not (~) value of the current BigInteger. Calculated as (~this).
Syntax : public BigInteger not()
 
BigInteger or(BigInteger val) This method is used to find out the bitwise or value from the current BigInteger value and the given BigInteger value.
Syntax : public BigInteger or(BigInteger val)
 
BigInteger pow(int exponent) This method is used to find out the value after calculating the exponent of the current BigInteger.
Syntax : public BigInteger pow(int exponent)
 
static BigInteger probablePrime(int bitLength, Random rnd) This method is used to find out the positive BigInteger value which is probable prime with the given bitLength.
Syntax : public static BigInteger probablePrime(int bitLength, Random rnd)
 
BigInteger remainder(BigInteger val) This method is used to find out the remainder from the current BigInteger value and the given BigInteger value. Calculated as (this % val)
Syntax : public BigInteger remainder(BigInteger val)
 
BigInteger setBit(int n) This method is used to find out the BigInteger whose value is equivalent to the current BigInteger with the given bit set.
Syntax : public BigInteger setBit(int n)
 
BigInteger shiftLeft(int n) This method is used to shift the current BigInteger value to left by given bit as (this << n).
Syntax : public BigInteger shiftLeft(int n)
 
BigInteger shiftRight(int n) This method is used to shift the current BigInteger value to right by given bit as (this >> n).
Syntax : public BigInteger shiftRight(int n)
 
int signum() This method is used to find out the signum function of the current BigInteger value.
Syntax : public int signum()
 
BigInteger subtract(BigInteger val) This method is used to subtract the current BigInteger value and the given BigInteger value.
Syntax : public BigInteger subtract(BigInteger val)
 
boolean testBit(int n) This method is used to test whether the given bit is set or not.
Syntax : public boolean testBit(int n)
 
byte[] toByteArray() This method is used to find out the byte array of the current BigInteger value.
Syntax : public byte[] toByteArray()
 
String toString() This method is used to find out the String representation of the current BigInteger.
Syntax : public String toString()
 
String toString(int radix) This method is used to find out the String representation of the current BigInteger in the given radix.
Syntax : public String toString(int radix)
 
static BigInteger valueOf(long val) This method is used to find out the BigInteger value of the given value as long.
Syntax : public static BigInteger valueOf(long val)
 
BigInteger xor(BigInteger val) This method is used to find out the xor value of the current BigInteger value and given BigInteger value. Calculated as (this ^ val).
Syntax : public BigInteger xor(BigInteger val)

Example

Here I am giving a simple example which will demonstrate you about how to use BigInteger in you application. In this example we will create a Java class where we will create the BigInteger objects then we will perform operations on these BigInteger's values.

BigIntegerExample.java


import java.math.BigInteger;
public class BigIntegerExample {

	public static void main(String[] args) 
	  {
	  BigInteger bigInteger1 = new BigInteger ("6");
	  BigInteger bigInteger2 = new BigInteger ("2");
	  BigInteger bigIntAdd = bigInteger1.add(bigInteger2);
	  BigInteger bigIntSub = bigInteger1.subtract(bigInteger2);
	  BigInteger bigIntMultiply = bigInteger1.multiply(bigInteger2);
	  BigInteger bigIntDivide = bigInteger1.divide(bigInteger2);
	  System.out.println("BigInteger Addition  ==> " + bigIntAdd);	
	  System.out.println("BigInteger Subtract  ==> " + bigIntSub);
	  System.out.println("BigInteger Multiply  ==> " + bigIntMultiply);
	  System.out.println("BigInteger Division  ==> " + bigIntDivide);
	  System.out.println("BigInteger Negate "+bigInteger1+"   ==> " +bigInteger1.negate());
	  }
}

Output :

When you will compile and execute the above example you will get the output as follows :

Download Source Code