BigInteger Mathematical Operations Examples


BigInteger Mathematical Operations Examples

BigInteger is in an immutable object, so you need to assign the results of any mathematical operation, to a new BigInteger instance.

import java.math.BigInteger;
 
public class BigIntegerOperations {
    public static void main(String[] args) {
        // Addition: 20 + 20 = 40
        BigInteger num1 = new BigInteger("20");
        BigInteger num2 = new BigInteger("20");
        BigInteger sum = num1.add(num2);
        System.out.println("Addition: " + sum);
 
        // Subtraction: 20 - 18 = 2
        BigInteger num3 = new BigInteger("20");
        BigInteger num4 = new BigInteger("18");
        BigInteger sub = num3.subtract(num4);
        System.out.println("Subtraction: " + sub);
 
        // Division: 20 / 5 = 4
        BigInteger num5 = new BigInteger("20");
        BigInteger num6 = new BigInteger("5");
        BigInteger div = num5.divide(num6);
        System.out.println("Division: " + div);
 
        // Multiplication: 20 * 5 = 100
        BigInteger num7 = new BigInteger("20");
        BigInteger num8 = new BigInteger("5");
        BigInteger mul = num7.multiply(num8);
        System.out.println("Multiplication: " + mul);
 
        // Power: 20 ^ 3 = 8000
        BigInteger num9 = new BigInteger("20");
        BigInteger power = num9.pow(3);
        System.out.println("Power: " + power);
 
        // Remainder: 20 % 6 = 2
        BigInteger num10 = new BigInteger("20");
        BigInteger num11 = new BigInteger("6");
        BigInteger remainder = num10.remainder(num11);
        System.out.println("Remainder: " + remainder);
 
        // GCD: Greatest Common Divisor (GCD) for 24 and 36 is 12
        BigInteger num12 = new BigInteger("24");
        BigInteger num13 = new BigInteger("36");
        BigInteger gcd = num12.gcd(num13);
        System.out.println("GCD: " + gcd);
 
        // Maximum of two BigIntegers
        BigInteger num14 = new BigInteger("20");
        BigInteger num15 = new BigInteger("25");
        BigInteger max = num14.max(num15);
        System.out.println("Maximum: " + max);
 
        // Minimum of two BigIntegers
        BigInteger num16 = new BigInteger("20");
        BigInteger num17 = new BigInteger("25");
        BigInteger min = num16.min(num17);
        System.out.println("Minimum: " + min);
    }
}
 

Binary Logic Operations on BigInteger

BigInteger supports the binary logic operations that are available to Number types as well. As with all operations they are implemented by calling a method.

import java.math.BigInteger;
 
public class BigIntegerOperations {
    public static void main(String[] args) {
        // Binary Or: 20 | 15 = 31
        BigInteger num1 = new BigInteger("20");
        BigInteger num2 = new BigInteger("15");
        BigInteger orResult = num1.or(num2);
        System.out.println("Binary Or: " + orResult);
 
        // Binary And: 20 & 15 = 4
        BigInteger num3 = new BigInteger("20");
        BigInteger num4 = new BigInteger("15");
        BigInteger andResult = num3.and(num4);
        System.out.println("Binary And: " + andResult);
 
        // Binary Xor: 20 ^ 15 = 27
        BigInteger num5 = new BigInteger("20");
        BigInteger num6 = new BigInteger("15");
        BigInteger xorResult = num5.xor(num6);
        System.out.println("Binary Xor: " + xorResult);
 
        // Right Shift: 20 >> 2 = 5
        BigInteger num7 = new BigInteger("20");
        BigInteger rightShift = num7.shiftRight(2);
        System.out.println("Right Shift: " + rightShift);
 
        // Left Shift: 20 << 2 = 80
        BigInteger num8 = new BigInteger("20");
        BigInteger leftShift = num8.shiftLeft(2);
        System.out.println("Left Shift: " + leftShift);
 
        // Binary Inversion (Not): ~20 = -21
        BigInteger num9 = new BigInteger("20");
        BigInteger notResult = num9.not();
        System.out.println("Binary Inversion (Not): " + notResult);
 
        // NAND (And-Not): 20 & ~15 = 5
        BigInteger num10 = new BigInteger("20");
        BigInteger num11 = new BigInteger("15");
        BigInteger nandResult = num10.andNot(num11);
        System.out.println("NAND (And-Not): " + nandResult);
    }
}
 

Basic Programs