Java BigInteger Class Methods with Examples

In this article, we will discuss important methods or APIs of the Java BigInteger class from the java.math package.
The java.math.BigInteger class provides operations analogs 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 twos-complement notation.
The semantics of arithmetic operations exactly mimic those of Java's integer arithmetic operators, as defined in The Java Language Specification. For example, division by zero throws an ArithmeticException and division of a negative by a positive yields a negative (or zero) remainder. All of the details in the Spec concerning overflow are ignored, as BigIntegers are made as large as necessary to accommodate the results of an operation.
Check out below Java Core API packages tutorials:

Java BigInteger Class Diagram

The below class diagram shows a list of methods that the BigInteger class provides.

Java BigInteger Class Methods/APIs with Examples

Let's demonstrates the usage of a few important and commonly used BigInteger class methods with examples. In below BigIntegerMethodsExample class, each method name describes the BigInteger class method and its usage. Check out each BigInteger class method description at Oracle Java API Doc.
package net.javaguides.math;

import java.math.BigInteger;
import java.util.Random;

/**
 * Class demonstrates the usage of BigInteger class methods with examples
 * 
 * @author Ramesh Fadatare
 *
 */
public class BigIntegerMethodsExample {
    public static void main(String[] args) {
        BigIntegerMethodsExample example = new BigIntegerMethodsExample();
        example.xorMethod();
        example.valueOfMethod();
        example.toStringMethod();
        example.toByteArrayMethod();
        example.testBitMethod();
        example.subtractMethod();
        example.signumMethod();
        example.shiftRightMethod();
        example.shiftLeftMethod();
        example.setBitMethod();
        example.remainderMethod();
        example.probablePrimeMethod();
        example.orMethod();
        example.notMethod();
        example.negateMethod();
        example.multiplyMethod();
        example.modMethod();
        example.minMethod();
        example.maxMethod();
        example.intValueMethod();
        example.floatValueMethod();
        example.doubleValueMethod();
        example.divideMethod();
        example.andMethod();
        example.addMethod();
        example.absMethod();
    }

    public void xorMethod() {

        BigInteger bi1 = new BigInteger("8");
        BigInteger bi2 = new BigInteger("-6");

        // perform xor on bi1, bi2 and assign result to bi3
        BigInteger bi3 = bi1.xor(bi2);

        String str = "XOR operation " + bi1 + " and " + bi2 + " gives " + bi3;

        // print bi3 value
        System.out.println(str);
    }

    public void valueOfMethod() {
        // create and assign value to Long object
        Long l = new Long(123456789 L);

        // assign the biginteger value of l to bi
        // static method is called using class name
        BigInteger bi = BigInteger.valueOf(l);

        String str = "BigIntger value of Long " + l + " is " + bi;

        // print bi value
        System.out.println(str);
    }

    public void toStringMethod() {
        BigInteger bi1 = new BigInteger("1234");
        BigInteger bi2 = new BigInteger("-1234");

        // assign String value of bi1, bi2 to s1, s2
        String s1 = bi1.toString();
        String s2 = bi2.toString();

        String str1 = "String value of " + bi1 + " is " + s1;
        String str2 = "String value of " + bi2 + " is " + s2;

        // print s1, s2 values
        System.out.println(str1);
        System.out.println(str2);
    }

    public void toByteArrayMethod() {
        // create and assign value to byte array b3
        byte b3[] = {
            0x1,
            0x00,
            0x00
        };

        BigInteger bi1 = new BigInteger("10");
        BigInteger bi2 = new BigInteger(b3); // using byte[] constructor of BigInteger

        // assign byte array representation of bi1, bi2 to b1, b2
        byte[] b1 = bi1.toByteArray();
        byte[] b2 = bi2.toByteArray();

        String str1 = "Byte array representation of " + bi1 + " is: ";

        System.out.println(str1);

        // print byte array b1 using for loop
        for (int i = 0; i < b1.length; i++) {
            System.out.format("0x%02X\n", b1[i]);
        }

        String str2 = "Byte array representation of " + bi2 + " is: ";

        System.out.println(str2);

        // print byte array b2 using for loop
        for (int j = 0; j < b2.length; j++) {
            System.out.format("0x%02X ", b2[j]);
        }
    }

    public void testBitMethod() {
        BigInteger bi = new BigInteger("10");
        // perform testbit on bi at index 2 and 3
        Boolean b1 = bi.testBit(2);
        Boolean b2 = bi.testBit(3);

        String str1 = "Test Bit on " + bi + " at index 2 returns " + b1;
        String str2 = "Test Bit on " + bi + " at index 3 returns " + b2;

        // print b1, b2 values
        System.out.println(str1);
        System.out.println(str2);
    }

    public void subtractMethod() {
        // create 3 BigInteger objects
        BigInteger bi1, bi2, bi3;

        bi1 = new BigInteger("123");
        bi2 = new BigInteger("-123");

        // assign difference of bi1 and bi2 to bi3
        bi3 = bi1.subtract(bi2);

        String str = "Subtraction result: " + bi1 + " - " + bi2 + " = " + bi3;

        // print bi3 value
        System.out.println(str);
    }

    public void signumMethod() {
        // create 3 BigInteger objects
        BigInteger bi1, bi2, bi3;

        // create 3 int objects
        int i1, i2, i3;

        bi1 = new BigInteger("0");
        bi2 = new BigInteger("10");
        bi3 = new BigInteger("-10");

        // assign signum results of bi1, bi2, bi3 to i1, i2, i3
        i1 = bi1.signum();
        i2 = bi2.signum();
        i3 = bi3.signum();

        String str1 = "Signum function returns " + i1 + " for " + bi1;
        String str2 = "Signum function returns " + i2 + " for " + bi2;
        String str3 = "Signum function returns " + i3 + " for " + bi3;

        // print i1, i2, i3 values
        System.out.println(str1);
        System.out.println(str2);
        System.out.println(str3);
    }

    public void shiftRightMethod() {
        // create 3 BigInteger objects
        BigInteger bi1, bi2, bi3;

        bi1 = new BigInteger("4");

        // perform right shift operation on bi1 using 2 and -2
        bi2 = bi1.shiftRight(2);
        bi3 = bi1.shiftRight(-2);

        String str1 = "Right shift on " + bi1 + ", 2 times gives " + bi2;
        String str2 = "Right shift on " + bi1 + ", -2 times gives " + bi3;

        // print bi2, bi3 values
        System.out.println(str1);
        System.out.println(str2);
    }

    public void shiftLeftMethod() {
        // create 3 BigInteger objects
        BigInteger bi1, bi2, bi3;

        bi1 = new BigInteger("10");

        // perform leftshift operation on bi1 using 2 and -2
        bi2 = bi1.shiftLeft(2);
        bi3 = bi1.shiftLeft(-2);

        String str1 = "Leftshift on " + bi1 + ", 2 times gives " + bi2;
        String str2 = "Leftshift on " + bi1 + ",-2 times gives " + bi3;

        // print bi2, bi3 values
        System.out.println(str1);
        System.out.println(str2);
    }

    public void setBitMethod() {
        // create 2 BigInteger objects
        BigInteger bi1, bi2;

        bi1 = new BigInteger("7");

        // perform setbit operation on bi1 using index 3
        bi2 = bi1.setBit(3);

        String str = "Setbit operation on " + bi1 + " at index 3 gives " + bi2;

        // print bi2 value
        System.out.println(str);
    }

    public void remainderMethod() {
        // create 3 BigInteger objects
        BigInteger bi1, bi2, bi3;

        bi1 = new BigInteger("-100");
        bi2 = new BigInteger("3");

        // perform remainder operation on bi1 using bi2
        bi3 = bi1.remainder(bi2);

        String str = bi1 + " % " + bi2 + " is " + bi3;

        // print bi3 value
        System.out.println("Remainder result for " + str);
    }

    public void probablePrimeMethod() {
        // create a BigInteger object
        BigInteger bi;

        // create and assign value to bitLength
        int bitLength = 3;

        // create a random object
        Random rnd = new Random();

        // assign probablePrime result to bi using bitLength and rnd
        // static method is called using class name
        bi = BigInteger.probablePrime(bitLength, rnd);

        String str = "ProbablePrime of bitlength " + bitLength + " is " + bi;

        // print bi value
        System.out.println(str);
    }

    public void orMethod() {
        // create 3 BigInteger objects
        BigInteger bi1, bi2, bi3;

        // assign values to bi1, bi2
        bi1 = new BigInteger("6");
        bi2 = new BigInteger("8");

        // perform or operation on bi1, bi2
        bi3 = bi1.or(bi2);

        String str = "OR operation on " + bi1 + " and " + bi2 + " gives " + bi3;

        // print bi3 value
        System.out.println(str);
    }

    public void notMethod() {
        // create 4 BigInteger objects
        BigInteger bi1, bi2, bi3, bi4;

        // assign values to bi1, bi2
        bi1 = new BigInteger("6");
        bi2 = new BigInteger("-6");

        // perform not operation on bi1 and bi2
        bi3 = bi1.not();
        bi4 = bi2.not();

        String str1 = "Result of not operation on " + bi1 + " gives " + bi3;
        String str2 = "Result of not operation on " + bi2 + " gives " + bi4;

        // print bi3, bi4 values
        System.out.println(str1);
        System.out.println(str2);
    }

    public void negateMethod() {
        // create 2 BigInteger objects
        BigInteger bi1, bi2;

        bi1 = new BigInteger("20");

        // assign negate value of bi1 to bi2
        bi2 = bi1.negate();

        String str = "Negated value of " + bi1 + " is " + bi2;

        // print bi2 value
        System.out.println(str);
    }

    public void multiplyMethod() {
        // create 3 BigInteger objects
        BigInteger bi1, bi2, bi3;

        bi1 = new BigInteger("7");
        bi2 = new BigInteger("20");

        // multiply bi1 with bi2 and assign result to bi3
        bi3 = bi1.multiply(bi2);

        String str = bi1 + " * " + bi2 + " = " + bi3;

        // print bi3 value
        System.out.println("Multiplication result is " + str);
    }

    public void modMethod() {
        // create 3 BigInteger objects
        BigInteger bi1, bi2, bi3;

        bi1 = new BigInteger("-100");
        bi2 = new BigInteger("3");

        // perform mod operation on bi1 using bi2
        bi3 = bi1.mod(bi2);

        String str = bi1 + " mod " + bi2 + " is " + bi3;

        // print bi3 value
        System.out.println(str);
    }

    public void minMethod() {
        // create 3 BigInteger objects
        BigInteger bi1, bi2, bi3;

        bi1 = new BigInteger("123");
        bi2 = new BigInteger("1000");

        // assign the min value of bi1, bi2 to bi3
        bi3 = bi1.min(bi2);

        String str = "Minimum Value among " + bi1 + " and " + bi2 + " is " + bi3;

        // print the minimum value
        System.out.println(str);
    }

    public void maxMethod() {
        // create 3 BigInteger objects
        BigInteger bi1, bi2, bi3;

        bi1 = new BigInteger("123");
        bi2 = new BigInteger("1000");

        // assign the max value of bi1, bi2 to bi3
        bi3 = bi1.max(bi2);

        String str = "Maximum Value among " + bi1 + " and " + bi2 + " is " + bi3;

        // print the maximum value
        System.out.println(str);
    }

    public void intValueMethod() {
        // create 2 BigInteger objects
        BigInteger bi1, bi2;

        // assign values to bi1, bi2
        bi1 = new BigInteger("100");
        bi2 = new BigInteger("988");

        // assign the integer values of bi1, bi2 to i1, i2
        Integer i1 = bi1.intValue();
        Integer i2 = bi2.intValue();

        String str1 = "Integer value of " + bi1 + " is " + i1;
        String str2 = "Integer value of " + bi2 + " is " + i2;

        // print i1, i2 values
        System.out.println(str1);
        System.out.println(str2);

    }

    public void floatValueMethod() {
        // create 2 BigInteger objects
        BigInteger bi1, bi2;

        // assign values to bi1, bi2
        bi1 = new BigInteger("123");
        bi2 = new BigInteger("-123");

        // assign float values of bi1, bi2 to f1, f2
        Float f1 = bi1.floatValue();
        Float f2 = bi2.floatValue();

        String str1 = "Float value of " + bi1 + " is " + f1;
        String str2 = "Float value of " + bi2 + " is " + f2;

        // print f1, f2 values
        System.out.println(str1);
        System.out.println(str2);
    }

    public void doubleValueMethod() {

        // assign value to bi1
        BigInteger bi1 = new BigInteger("123");

        // assign a larger value to bi2
        BigInteger bi2 = new BigInteger("12345678");

        // assign double value of bi1, bi2 to d1, d2
        Double d1 = bi1.doubleValue();
        Double d2 = bi2.doubleValue();

        String str1 = "Double value of " + bi1 + " is " + d1;
        String str2 = "Double value of " + bi2 + " is " + d2;

        // print d1, d2 values
        System.out.println(str1);
        System.out.println(str2);

    }

    public void divideMethod() {
        // create 3 BigInteger objects
        BigInteger bi1, bi2, bi3;

        bi1 = new BigInteger("-100");
        bi2 = new BigInteger("3");

        // divide bi1 with bi2
        bi3 = bi1.divide(bi2);

        String str = "Division result is " + bi3;

        // print bi3 value
        System.out.println(str);
    }

    public void andMethod() {
        // create 3 BigInteger objects
        BigInteger bi1, bi2, bi3;

        // assign values to bi1, bi2
        bi1 = new BigInteger("6"); // 110
        bi2 = new BigInteger("3"); // 011

        // perform and operation on bi1 using bi2
        bi3 = bi1.and(bi2);

        String str = "Result of and operation is " + bi3;;

        // print bi3 value
        System.out.println(str);
    }

    public void addMethod() {
        // create 3 BigInteger objects
        BigInteger bi1, bi2, bi3;

        // assign values to bi1, bi2
        bi1 = new BigInteger("123");
        bi2 = new BigInteger("50");

        // perform add operation on bi1 using bi2
        bi3 = bi1.add(bi2);

        String str = "Result of addition is " + bi3;;

        // print bi3 value
        System.out.println(str);
    }

    public void absMethod() {
        // create 4 BigInteger objects
        BigInteger bi1, bi2, bi3, bi4;

        // assign values to bi1, bi2
        bi1 = new BigInteger("123");
        bi2 = new BigInteger("-123");

        // assign absolute values of bi1, bi2 to bi3, bi4
        bi3 = bi1.abs();
        bi4 = bi2.abs();

        String str1 = "Absolute value of " + bi1 + " is " + bi3;
        String str2 = "Absolute value of " + bi2 + " is " + bi4;

        // print bi3, bi4 values
        System.out.println(str1);
        System.out.println(str2);
    }
}
Output:
XOR operation 8 and -6 gives -14
BigIntger value of Long 123456789 is 123456789
String value of 1234 is 1234
String value of -1234 is -1234
Byte array representation of 10 is: 
0x0A
Byte array representation of 65536 is: 
0x01 0x00 0x00 Test Bit on 10 at index 2 returns false
Test Bit on 10 at index 3 returns true
Subtraction result: 123 - -123 = 246
Signum function returns 0 for 0
Signum function returns 1 for 10
Signum function returns -1 for -10
Right shift on 4, 2 times gives 1
Right shift on 4, -2 times gives 16
Leftshift on 10, 2 times gives 40
Leftshift on 10,-2 times gives 2
Setbit operation on 7 at index 3 gives 15
Remainder result for -100 % 3 is -1
ProbablePrime of bitlength 3 is 5
OR operation on 6 and 8 gives 14
Result of not operation on 6 gives -7
Result of not operation on -6 gives 5
Negated value of 20 is -20
Multiplication result is 7 * 20 = 140
-100 mod 3 is 2
Minimum Value among 123 and 1000 is 123
Maximum Value among 123 and 1000 is 1000
Integer value of 100 is 100
Integer value of 988 is 988
Float value of 123 is 123.0
Float value of -123 is -123.0
Double value of 123 is 123.0
Double value of 12345678 is 1.2345678E7
Division result is -33
Result of and operation is 2
Result of addition is 173
Absolute value of 123 is 123
Absolute value of -123 is 123

References


Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours


Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course

Comments