Best 10 java.math.BigInteger class Examples with tutorials

This tutorial shows multiple examples of the BigInteger class in Java.

BigInteger is a Java class defined in java.math package. It stores large numbers with unlimited value ranges.

You can also check my previous posts on the BigInteger class in Java.

How to Convert BigInteger to binary number?

BigInteger has the toString(radix) method, which takes the radix parameter.

The radix value must be between Characters.MIN_RADIX(=2) and Character.MAX_RADIX(=36), radix is out of this range, it defaults to radix=10

public String toString(int radix)

A binary String is a string that contains digits of zero and one. It is represented in 2 base numeric values,

To convert from BigInteger to a binary string, the Radix value must be passed as 2.

System.out.println(new BigInteger("1").toString(2));
System.out.println(new BigInteger("5").toString(2));
System.out.println(new BigInteger("12").toString(2));
System.out.println(new BigInteger("131").toString(2));

Output:

1
101
1100
10000011

Convert BigInteger to Octal number?

The octal string is represented in 8 base numeric values.

The `BigInteger.toString(radix)“ method takes the radix value as 8 for Octal String conversion.

So, the Radix value must be passed as 8 to convert to an Octal number

System.out.println(new BigInteger("1").toString(8));
System.out.println(new BigInteger("5").toString(8));
System.out.println(new BigInteger("12").toString(8));
System.out.println(new BigInteger("64").toString(8));

Output is

1
5
14
100

Convert BigInteger to Hexadecimal Number?

Hexadecimal is represented in 16 base numeric values.

The BigInteger.toString(radix) method takes the radix value as 16 for Hexadecimal Number Conversion. The Radix value must be passed as 16.

System.out.println(new BigInteger("11").toString(16));
System.out.println(new BigInteger("51").toString(16));
System.out.println(new BigInteger("112").toString(16));
System.out.println(new BigInteger("164").toString(16));

Output:

b
33
70
a4

power of exponent on Biginteger

BigInteger Pow() method is used to calculate the power of an exponent on a big integer.

BigInteger class pow() method, Syntax is

public BigInteger pow(int exponent)

returns a big integer value which is this power exponent. The parameter exponent is an integer

Here is an example for java.math.BigInteger.pow() method

BigInteger bigInteger=new BigInteger("5");
System.out.println(bigInteger.pow(1));
System.out.println(bigInteger.pow(3));
System.out.println(bigInteger.pow(5));

Output:

5
125
3125

BigInteger multiply() method example

This class multiply() method does the multiplication of big integer values and returns big integer objects.

public BigInteger multiply(BigInteger val)

Here is an example of java.math.BigInteger.multiply() method

BigInteger bigInteger=new BigInteger("5");
BigInteger bigInteger1=new BigInteger("6");
System.out.println(bigInteger.multiply(bigInteger1));

The output of the above code is

30

Division and Remainder BigInteger Examples

This class below methods division and the remainder on biginteger values.

  • The divide() method does divide big integer values with this and returns a big integer value
  • The remainder() method does remainder on big integer values with this and returns a big integer value.
public BigInteger divide(BigInteger val) {

Following is a code example for Java. math.BigInteger.divide() method

BigInteger bigInteger1 = new BigInteger("87945697851122");
BigInteger bigInteger2 = BigInteger.valueOf(587955);
System.out.println(bigInteger1.divide(bigInteger2));
System.out.println(bigInteger1.remainder(bigInteger2));

output

149578960
424322

BigInteger CompareTo() method Examples

It has the CompareTo() method which compares two BigIntegers, and returns an integer - 0,-1,1.

  • 0, Both values are equal
  • 1 - called object value is more than the compared value
  • -1 - called object value is less than the compared value.
public int compareTo(BigInteger val)

Following is a code example for java.math.BigInteger.compareTo() method

BigInteger bigInteger=new BigInteger("5");
System.out.println(bigInteger.compareTo(new BigInteger("5")));
System.out.println(bigInteger.compareTo(new BigInteger("12")));
System.out.println(bigInteger.compareTo(new BigInteger("2")));

Output is

0
-1
1

Add and Subtraction of Biginger numbers

Bigintegers needs methods to do athematic operations

  • add(): addition of big numbers, return result.
  • subtract(): Subtract big numbers, return result Following is an example of these methods
import java.math.BigDecimal;
import java.math.BigInteger;
public class Test11 {
    public static void main(String[] args) {
      BigInteger one=new BigInteger("15");
      BigInteger two=new BigInteger("5");
      System.out.println(one.add(two));
      System.out.println(one.subtract(two));

    }

}```