When working with cryptographic keys, factorial calculations, or any computation involving numbers larger than 9 quintillion, primitive types overflow. BigInteger provides arbitrary-precision integer arithmetic, representing integers of any size limited only by available memory.

Creating BigInteger

Create BigInteger from strings or primitive values.

Create.java
// Create BigInteger

import java.math.BigInteger;

public class Create {
    public static void main(String[] args) {
        // From string
        BigInteger big1 = new BigInteger("12345678901234567890");
        System.out.println("From string: " + big1);

        // From long
        BigInteger big2 = BigInteger.valueOf(100);
        System.out.println("From long: " + big2);

        // From int (converts to long first)
        int value = 42;
        BigInteger big3 = BigInteger.valueOf(value);
        System.out.println("From int: " + big3);

        // Very large number
        String largeNum = "99999999999999999999999999999999999999999999999999";
        BigInteger veryBig = new BigInteger(largeNum);
        System.out.println("Very large: " + veryBig);

        // Constants
        System.out.println("\nConstants:");
        System.out.println("ZERO: " + BigInteger.ZERO);
        System.out.println("ONE: " + BigInteger.ONE);
        System.out.println("TWO: " + BigInteger.TWO);
        System.out.println("TEN: " + BigInteger.TEN);

        // From byte array
        byte[] bytes = {1, 2, 3};
        BigInteger fromBytes = new BigInteger(bytes);
        System.out.println("\nFrom bytes: " + fromBytes);

        // Negative numbers
        BigInteger negative = new BigInteger("-12345");
        System.out.println("\nNegative: " + negative);

        // With radix (base)
        BigInteger hex = new BigInteger("FF", 16);
        System.out.println("Hex FF: " + hex);
        
        BigInteger binary = new BigInteger("1111", 2);
        System.out.println("Binary 1111: " + binary);

        // probablePrime
        BigInteger prime = BigInteger.probablePrime(20, new java.util.Random(42));
        System.out.println("\nProbable prime (20 bits): " + prime);

        // Convert to primitives
        System.out.println("\nConvert to primitives:");
        BigInteger bi = BigInteger.valueOf(12345);
        System.out.println("intValue: " + bi.intValue());
        System.out.println("longValue: " + bi.longValue());
        System.out.println("doubleValue: " + bi.doubleValue());

        // String representations
        System.out.println("\nString representations:");
        BigInteger num = new BigInteger("255");
        System.out.println("Decimal: " + num.toString());
        System.out.println("Binary: " + num.toString(2));
        System.out.println("Hex: " + num.toString(16));
        System.out.println("Octal: " + num.toString(8));

        // Compare sizes
        System.out.println("\nCompare sizes:");
        System.out.println("Long.MAX_VALUE: " + Long.MAX_VALUE);
        BigInteger beyondLong = new BigInteger(String.valueOf(Long.MAX_VALUE)).add(BigInteger.ONE);
        System.out.println("Beyond long: " + beyondLong);
    }

    //help h1
    // new BigInteger(string) - from string
    // BigInteger.valueOf(long) - from long
    // BigInteger.ZERO, ONE, TWO, TEN - constants
    // new BigInteger(string, radix) - from string in base
    // .intValue(), .longValue() - convert to primitive
    // .toString(radix) - convert to string in base
    //end
}
BigInteger An immutable class for arbitrary-precision integers that never overflow, using method calls instead of operators for arithmetic.
Immutability BigInteger operations return new objects rather than modifying existing ones, so always capture the return value.

Arithmetic Operations

Perform basic math using method calls instead of operators.

Arithmetic.java
// Basic arithmetic

import java.math.BigInteger;

public class Arithmetic {
    public static void main(String[] args) {
        BigInteger a = new BigInteger("12345678901234567890");
        BigInteger b = new BigInteger("98765432109876543210");
        
        System.out.println("a = " + a);
        System.out.println("b = " + b);
        System.out.println();

        // Addition
        System.out.println("Addition:");
        BigInteger sum = a.add(b);
        System.out.println("a + b = " + sum);

        // Subtraction
        System.out.println("\nSubtraction:");
        BigInteger diff = b.subtract(a);
        System.out.println("b - a = " + diff);

        // Multiplication
        System.out.println("\nMultiplication:");
        BigInteger product = a.multiply(b);
        System.out.println("a * b = " + product);

        // Division
        System.out.println("\nDivision:");
        BigInteger quotient = b.divide(a);
        System.out.println("b / a = " + quotient);

        // Remainder (modulo)
        System.out.println("\nRemainder:");
        BigInteger remainder = b.remainder(a);
        System.out.println("b % a = " + remainder);

        // DivideAndRemainder
        System.out.println("\nDivide and remainder:");
        BigInteger[] divResult = b.divideAndRemainder(a);
        System.out.println("Quotient: " + divResult[0]);
        System.out.println("Remainder: " + divResult[1]);

        // Power
        System.out.println("\nPower:");
        BigInteger base = BigInteger.valueOf(2);
        BigInteger power = base.pow(100);
        System.out.println("2^100 = " + power);

        // Negate
        System.out.println("\nNegate:");
        BigInteger neg = a.negate();
        System.out.println("-a = " + neg);

        // Absolute value
        System.out.println("\nAbsolute value:");
        BigInteger negative = new BigInteger("-12345");
        System.out.println("abs(-12345) = " + negative.abs());

        // Increment/decrement
        System.out.println("\nIncrement/decrement:");
        BigInteger x = BigInteger.valueOf(10);
        System.out.println("x = " + x);
        System.out.println("x + 1 = " + x.add(BigInteger.ONE));
        System.out.println("x - 1 = " + x.subtract(BigInteger.ONE));

        // Chaining operations
        System.out.println("\nChaining:");
        BigInteger result = BigInteger.valueOf(5)
            .multiply(BigInteger.valueOf(3))
            .add(BigInteger.valueOf(10))
            .subtract(BigInteger.valueOf(2));
        System.out.println("5 * 3 + 10 - 2 = " + result);

        // Factorial example
        System.out.println("\nFactorial:");
        System.out.println("20! = " + factorial(20));
        System.out.println("50! = " + factorial(50));

        // Fibonacci example
        System.out.println("\nFibonacci:");
        System.out.println("fib(50) = " + fibonacci(50));
        System.out.println("fib(100) = " + fibonacci(100));
    }
    public static BigInteger factorial(int n) {
        BigInteger result = BigInteger.ONE;
        for (int i = 2; i <= n; i++) {
            result = result.multiply(BigInteger.valueOf(i));
        }
        return result;
    }
    public static BigInteger fibonacci(int n) {
        if (n <= 1) return BigInteger.valueOf(n);
        
        BigInteger a = BigInteger.ZERO;
        BigInteger b = BigInteger.ONE;
        
        for (int i = 2; i <= n; i++) {
            BigInteger temp = a.add(b);
            a = b;
            b = temp;
        }
        return b;
    }

    //help h1
    // .add(other) - addition
    // .subtract(other) - subtraction
    // .multiply(other) - multiplication
    // .divide(other) - division
    // .remainder(other) - modulo
    // .pow(exp) - power
    // .negate() - negation
    // .abs() - absolute value
    // Immutable: operations return new BigInteger
    //end
}

Comparison Operations

Compare BigInteger values using methods.

Comparison.java
// Comparison operations

import java.math.BigInteger;

public class Comparison {
    public static void main(String[] args) {
        BigInteger a = new BigInteger("100");
        BigInteger b = new BigInteger("200");
        BigInteger c = new BigInteger("100");
        
        System.out.println("a = " + a);
        System.out.println("b = " + b);
        System.out.println("c = " + c);
        System.out.println();

        // compareTo
        System.out.println("compareTo:");
        System.out.println("a.compareTo(b): " + a.compareTo(b));  // -1
        System.out.println("b.compareTo(a): " + b.compareTo(a));  // 1
        System.out.println("a.compareTo(c): " + a.compareTo(c));  // 0

        // equals
        System.out.println("\nequals:");
        System.out.println("a.equals(b): " + a.equals(b));
        System.out.println("a.equals(c): " + a.equals(c));

        // Comparison helpers
        System.out.println("\nComparison helpers:");
        System.out.println("a < b: " + (a.compareTo(b) < 0));
        System.out.println("a <= b: " + (a.compareTo(b) <= 0));
        System.out.println("a > b: " + (a.compareTo(b) > 0));
        System.out.println("a >= b: " + (a.compareTo(b) >= 0));
        System.out.println("a == c: " + (a.compareTo(c) == 0));

        // max and min
        System.out.println("\nmax and min:");
        System.out.println("max(a, b): " + a.max(b));
        System.out.println("min(a, b): " + a.min(b));

        // signum
        System.out.println("\nsignum:");
        System.out.println("signum(100): " + a.signum());
        System.out.println("signum(-100): " + a.negate().signum());
        System.out.println("signum(0): " + BigInteger.ZERO.signum());

        // Find max in array
        System.out.println("\nFind max in array:");
        BigInteger[] numbers = {
            new BigInteger("12345"),
            new BigInteger("98765"),
            new BigInteger("45678"),
            new BigInteger("23456")
        };
        
        BigInteger max = findMax(numbers);
        BigInteger min = findMin(numbers);
        System.out.println("Max: " + max);
        System.out.println("Min: " + min);

        // Sort array
        System.out.println("\nSort array:");
        java.util.Arrays.sort(numbers);
        System.out.println("Sorted: " + java.util.Arrays.toString(numbers));

        // Check ranges
        System.out.println("\nCheck ranges:");
        BigInteger value = new BigInteger("150");
        BigInteger lower = new BigInteger("100");
        BigInteger upper = new BigInteger("200");
        
        boolean inRange = value.compareTo(lower) >= 0 && value.compareTo(upper) <= 0;
        System.out.println(value + " in range [" + lower + ", " + upper + "]: " + inRange);

        // Zero check
        System.out.println("\nZero check:");
        System.out.println("a equals ZERO: " + a.equals(BigInteger.ZERO));
        System.out.println("ZERO equals ZERO: " + BigInteger.ZERO.equals(BigInteger.ZERO));

        // Sign check
        System.out.println("\nSign check:");
        BigInteger pos = new BigInteger("100");
        BigInteger neg = new BigInteger("-100");
        System.out.println("pos > 0: " + (pos.signum() > 0));
        System.out.println("neg < 0: " + (neg.signum() < 0));
        System.out.println("ZERO == 0: " + (BigInteger.ZERO.signum() == 0));
    }
    public static BigInteger findMax(BigInteger[] arr) {
        BigInteger max = arr[0];
        for (BigInteger val : arr) {
            max = max.max(val);
        }
        return max;
    }
    public static BigInteger findMin(BigInteger[] arr) {
        BigInteger min = arr[0];
        for (BigInteger val : arr) {
            min = min.min(val);
        }
        return min;
    }

    //help h1
    // .compareTo(other) - returns -1, 0, or 1
    // .equals(other) - equality check
    // .max(other), .min(other)
    // .signum() - sign (-1, 0, or 1)
    // Use compareTo for ordering
    // Use equals for equality
    //end
}

Modular Arithmetic

Operations useful for cryptography and number theory.

Modular.java
// Modular arithmetic

import java.math.BigInteger;

public class Modular {
    public static void main(String[] args) {
        String modInput = ;
        BigInteger value = new BigInteger(modInput);
        BigInteger modulus = new BigInteger("5");
        
        System.out.println("value = " + value);
        System.out.println("modulus = " + modulus);
        System.out.println();

        // Mod
        System.out.println("mod:");
        BigInteger mod = value.mod(modulus);
        System.out.println(value + " mod " + modulus + " = " + mod);

        // ModPow (modular exponentiation)
        System.out.println("\nModPow (modular exponentiation):");
        BigInteger base = new BigInteger("3");
        BigInteger exponent = new BigInteger("4");
        BigInteger m = new BigInteger("5");
        BigInteger modPow = base.modPow(exponent, m);
        System.out.println(base + "^" + exponent + " mod " + m + " = " + modPow);

        // ModInverse
        System.out.println("\nModInverse:");
        BigInteger a = new BigInteger("3");
        BigInteger mod2 = new BigInteger("11");
        BigInteger inverse = a.modInverse(mod2);
        System.out.println("Inverse of " + a + " mod " + mod2 + " = " + inverse);
        System.out.println("Verify: " + a.multiply(inverse).mod(mod2));

        // GCD
        System.out.println("\nGCD:");
        BigInteger x = new BigInteger("48");
        BigInteger y = new BigInteger("18");
        BigInteger gcd = x.gcd(y);
        System.out.println("gcd(" + x + ", " + y + ") = " + gcd);

        // Check coprime
        System.out.println("\nCheck coprime:");
        BigInteger p = new BigInteger("15");
        BigInteger q = new BigInteger("28");
        boolean coprime = p.gcd(q).equals(BigInteger.ONE);
        System.out.println(p + " and " + q + " are coprime: " + coprime);

        // Modular exponentiation (large numbers)
        System.out.println("\nLarge modular exponentiation:");
        BigInteger largeBase = new BigInteger("123456789");
        BigInteger largeExp = new BigInteger("987654321");
        BigInteger largeMod = new BigInteger("1000000007");
        BigInteger result = largeBase.modPow(largeExp, largeMod);
        System.out.println("Result: " + result);

        // Probability prime
        System.out.println("\nProbable prime:");
        BigInteger candidate = new BigInteger("101");
        boolean isPrime = candidate.isProbablePrime(100);
        System.out.println(candidate + " is probably prime: " + isPrime);

        // Generate probable prime
        System.out.println("\nGenerate probable prime:");
        BigInteger prime = BigInteger.probablePrime(64, new java.util.Random(42));
        System.out.println("Random 64-bit prime: " + prime);

        // Practical: RSA key generation (simplified)
        System.out.println("\nRSA-like calculation:");
        BigInteger p1 = new BigInteger("61");
        BigInteger p2 = new BigInteger("53");
        BigInteger n = p1.multiply(p2);
        BigInteger phi = p1.subtract(BigInteger.ONE).multiply(p2.subtract(BigInteger.ONE));
        BigInteger e = new BigInteger("17");
        BigInteger d = e.modInverse(phi);
        
        System.out.println("p = " + p1 + ", q = " + p2);
        System.out.println("n = " + n);
        System.out.println("φ(n) = " + phi);
        System.out.println("e = " + e);
        System.out.println("d = " + d);
        
        // Encrypt/decrypt
        BigInteger message = new BigInteger("42");
        BigInteger encrypted = message.modPow(e, n);
        BigInteger decrypted = encrypted.modPow(d, n);
        System.out.println("Message: " + message);
        System.out.println("Encrypted: " + encrypted);
        System.out.println("Decrypted: " + decrypted);
    }

    //help h1
    // .mod(m) - modulo
    // .modPow(exp, m) - (this^exp) mod m
    // .modInverse(m) - modular multiplicative inverse
    // .gcd(other) - greatest common divisor
    // .isProbablePrime(certainty) - primality test
    // .probablePrime(bitLength, rnd) - generate prime
    // Efficient for cryptography
    //end
}
// Modular arithmetic

import java.math.BigInteger;

public class Modular {
    public static void main(String[] args) {
        String modInput = ;
        BigInteger value = new BigInteger(modInput);
        BigInteger modulus = new BigInteger("5");
        
        System.out.println("value = " + value);
        System.out.println("modulus = " + modulus);
        System.out.println();

        // Mod
        System.out.println("mod:");
        BigInteger mod = value.mod(modulus);
        System.out.println(value + " mod " + modulus + " = " + mod);

        // ModPow (modular exponentiation)
        System.out.println("\nModPow (modular exponentiation):");
        BigInteger base = new BigInteger("3");
        BigInteger exponent = new BigInteger("4");
        BigInteger m = new BigInteger("5");
        BigInteger modPow = base.modPow(exponent, m);
        System.out.println(base + "^" + exponent + " mod " + m + " = " + modPow);

        // ModInverse
        System.out.println("\nModInverse:");
        BigInteger a = new BigInteger("3");
        BigInteger mod2 = new BigInteger("11");
        BigInteger inverse = a.modInverse(mod2);
        System.out.println("Inverse of " + a + " mod " + mod2 + " = " + inverse);
        System.out.println("Verify: " + a.multiply(inverse).mod(mod2));

        // GCD
        System.out.println("\nGCD:");
        BigInteger x = new BigInteger("48");
        BigInteger y = new BigInteger("18");
        BigInteger gcd = x.gcd(y);
        System.out.println("gcd(" + x + ", " + y + ") = " + gcd);

        // Check coprime
        System.out.println("\nCheck coprime:");
        BigInteger p = new BigInteger("15");
        BigInteger q = new BigInteger("28");
        boolean coprime = p.gcd(q).equals(BigInteger.ONE);
        System.out.println(p + " and " + q + " are coprime: " + coprime);

        // Modular exponentiation (large numbers)
        System.out.println("\nLarge modular exponentiation:");
        BigInteger largeBase = new BigInteger("123456789");
        BigInteger largeExp = new BigInteger("987654321");
        BigInteger largeMod = new BigInteger("1000000007");
        BigInteger result = largeBase.modPow(largeExp, largeMod);
        System.out.println("Result: " + result);

        // Probability prime
        System.out.println("\nProbable prime:");
        BigInteger candidate = new BigInteger("101");
        boolean isPrime = candidate.isProbablePrime(100);
        System.out.println(candidate + " is probably prime: " + isPrime);

        // Generate probable prime
        System.out.println("\nGenerate probable prime:");
        BigInteger prime = BigInteger.probablePrime(64, new java.util.Random(42));
        System.out.println("Random 64-bit prime: " + prime);

        // Practical: RSA key generation (simplified)
        System.out.println("\nRSA-like calculation:");
        BigInteger p1 = new BigInteger("61");
        BigInteger p2 = new BigInteger("53");
        BigInteger n = p1.multiply(p2);
        BigInteger phi = p1.subtract(BigInteger.ONE).multiply(p2.subtract(BigInteger.ONE));
        BigInteger e = new BigInteger("17");
        BigInteger d = e.modInverse(phi);
        
        System.out.println("p = " + p1 + ", q = " + p2);
        System.out.println("n = " + n);
        System.out.println("φ(n) = " + phi);
        System.out.println("e = " + e);
        System.out.println("d = " + d);
        
        // Encrypt/decrypt
        BigInteger message = new BigInteger("42");
        BigInteger encrypted = message.modPow(e, n);
        BigInteger decrypted = encrypted.modPow(d, n);
        System.out.println("Message: " + message);
        System.out.println("Encrypted: " + encrypted);
        System.out.println("Decrypted: " + decrypted);
    }

    //help h1
    // .mod(m) - modulo
    // .modPow(exp, m) - (this^exp) mod m
    // .modInverse(m) - modular multiplicative inverse
    // .gcd(other) - greatest common divisor
    // .isProbablePrime(certainty) - primality test
    // .probablePrime(bitLength, rnd) - generate prime
    // Efficient for cryptography
    //end
}
// Modular arithmetic

import java.math.BigInteger;

public class Modular {
    public static void main(String[] args) {
        String modInput = ;
        BigInteger value = new BigInteger(modInput);
        BigInteger modulus = new BigInteger("5");
        
        System.out.println("value = " + value);
        System.out.println("modulus = " + modulus);
        System.out.println();

        // Mod
        System.out.println("mod:");
        BigInteger mod = value.mod(modulus);
        System.out.println(value + " mod " + modulus + " = " + mod);

        // ModPow (modular exponentiation)
        System.out.println("\nModPow (modular exponentiation):");
        BigInteger base = new BigInteger("3");
        BigInteger exponent = new BigInteger("4");
        BigInteger m = new BigInteger("5");
        BigInteger modPow = base.modPow(exponent, m);
        System.out.println(base + "^" + exponent + " mod " + m + " = " + modPow);

        // ModInverse
        System.out.println("\nModInverse:");
        BigInteger a = new BigInteger("3");
        BigInteger mod2 = new BigInteger("11");
        BigInteger inverse = a.modInverse(mod2);
        System.out.println("Inverse of " + a + " mod " + mod2 + " = " + inverse);
        System.out.println("Verify: " + a.multiply(inverse).mod(mod2));

        // GCD
        System.out.println("\nGCD:");
        BigInteger x = new BigInteger("48");
        BigInteger y = new BigInteger("18");
        BigInteger gcd = x.gcd(y);
        System.out.println("gcd(" + x + ", " + y + ") = " + gcd);

        // Check coprime
        System.out.println("\nCheck coprime:");
        BigInteger p = new BigInteger("15");
        BigInteger q = new BigInteger("28");
        boolean coprime = p.gcd(q).equals(BigInteger.ONE);
        System.out.println(p + " and " + q + " are coprime: " + coprime);

        // Modular exponentiation (large numbers)
        System.out.println("\nLarge modular exponentiation:");
        BigInteger largeBase = new BigInteger("123456789");
        BigInteger largeExp = new BigInteger("987654321");
        BigInteger largeMod = new BigInteger("1000000007");
        BigInteger result = largeBase.modPow(largeExp, largeMod);
        System.out.println("Result: " + result);

        // Probability prime
        System.out.println("\nProbable prime:");
        BigInteger candidate = new BigInteger("101");
        boolean isPrime = candidate.isProbablePrime(100);
        System.out.println(candidate + " is probably prime: " + isPrime);

        // Generate probable prime
        System.out.println("\nGenerate probable prime:");
        BigInteger prime = BigInteger.probablePrime(64, new java.util.Random(42));
        System.out.println("Random 64-bit prime: " + prime);

        // Practical: RSA key generation (simplified)
        System.out.println("\nRSA-like calculation:");
        BigInteger p1 = new BigInteger("61");
        BigInteger p2 = new BigInteger("53");
        BigInteger n = p1.multiply(p2);
        BigInteger phi = p1.subtract(BigInteger.ONE).multiply(p2.subtract(BigInteger.ONE));
        BigInteger e = new BigInteger("17");
        BigInteger d = e.modInverse(phi);
        
        System.out.println("p = " + p1 + ", q = " + p2);
        System.out.println("n = " + n);
        System.out.println("φ(n) = " + phi);
        System.out.println("e = " + e);
        System.out.println("d = " + d);
        
        // Encrypt/decrypt
        BigInteger message = new BigInteger("42");
        BigInteger encrypted = message.modPow(e, n);
        BigInteger decrypted = encrypted.modPow(d, n);
        System.out.println("Message: " + message);
        System.out.println("Encrypted: " + encrypted);
        System.out.println("Decrypted: " + decrypted);
    }

    //help h1
    // .mod(m) - modulo
    // .modPow(exp, m) - (this^exp) mod m
    // .modInverse(m) - modular multiplicative inverse
    // .gcd(other) - greatest common divisor
    // .isProbablePrime(certainty) - primality test
    // .probablePrime(bitLength, rnd) - generate prime
    // Efficient for cryptography
    //end
}
Modular arithmetic Computing remainders and modular inverses, essential for encryption algorithms like RSA.

Bit Operations

Manipulate individual bits in large integers.

Bitops.java
// Bit operations

import java.math.BigInteger;

public class Bitops {
    public static void main(String[] args) {
        BigInteger a = new BigInteger("60");  // 111100 in binary
        BigInteger b = new BigInteger("13");  // 001101 in binary
        
        System.out.println("a = " + a + " (" + a.toString(2) + " binary)");
        System.out.println("b = " + b + " (" + b.toString(2) + " binary)");
        System.out.println();

        // AND
        System.out.println("Bitwise AND:");
        BigInteger and = a.and(b);
        System.out.println("a & b = " + and + " (" + and.toString(2) + ")");

        // OR
        System.out.println("\nBitwise OR:");
        BigInteger or = a.or(b);
        System.out.println("a | b = " + or + " (" + or.toString(2) + ")");

        // XOR
        System.out.println("\nBitwise XOR:");
        BigInteger xor = a.xor(b);
        System.out.println("a ^ b = " + xor + " (" + xor.toString(2) + ")");

        // NOT
        System.out.println("\nBitwise NOT:");
        BigInteger not = a.not();
        System.out.println("~a = " + not);

        // AND NOT
        System.out.println("\nAND NOT:");
        BigInteger andNot = a.andNot(b);
        System.out.println("a & ~b = " + andNot);

        // Shift left
        System.out.println("\nShift left:");
        BigInteger shiftLeft = a.shiftLeft(2);
        System.out.println("a << 2 = " + shiftLeft);
        System.out.println("(multiply by 4): " + a.multiply(BigInteger.valueOf(4)));

        // Shift right
        System.out.println("\nShift right:");
        BigInteger shiftRight = a.shiftRight(2);
        System.out.println("a >> 2 = " + shiftRight);
        System.out.println("(divide by 4): " + a.divide(BigInteger.valueOf(4)));

        // Test bit
        System.out.println("\nTest bit:");
        for (int i = 0; i < 8; i++) {
            System.out.println("Bit " + i + " of " + a + ": " + a.testBit(i));
        }

        // Set bit
        System.out.println("\nSet bit:");
        BigInteger setBit = BigInteger.ZERO.setBit(3);
        System.out.println("Set bit 3: " + setBit + " (" + setBit.toString(2) + ")");

        // Clear bit
        System.out.println("\nClear bit:");
        BigInteger clearBit = a.clearBit(2);
        System.out.println("Clear bit 2 of " + a + ": " + clearBit);

        // Flip bit
        System.out.println("\nFlip bit:");
        BigInteger flipBit = a.flipBit(0);
        System.out.println("Flip bit 0 of " + a + ": " + flipBit);

        // Bit count
        System.out.println("\nBit count:");
        System.out.println("Bit count of " + a + ": " + a.bitCount());
        System.out.println("Bit length of " + a + ": " + a.bitLength());

        // Lowest set bit
        System.out.println("\nLowest set bit:");
        System.out.println("Lowest set bit of " + a + ": " + a.getLowestSetBit());

        // Check even/odd
        System.out.println("\nEven/Odd:");
        System.out.println(a + " is even: " + !a.testBit(0));
        System.out.println(b + " is odd: " + b.testBit(0));

        // Powers of 2
        System.out.println("\nPowers of 2:");
        for (int i = 0; i <= 10; i++) {
            BigInteger power = BigInteger.ONE.shiftLeft(i);
            System.out.println("2^" + i + " = " + power);
        }
    }

    //help h1
    // .and(other) - bitwise AND
    // .or(other) - bitwise OR
    // .xor(other) - bitwise XOR
    // .not() - bitwise NOT
    // .shiftLeft(n) - left shift (multiply by 2^n)
    // .shiftRight(n) - right shift (divide by 2^n)
    // .testBit(n) - test if bit n is set
    // .setBit(n), .clearBit(n), .flipBit(n)
    // .bitCount(), .bitLength()
    //end
}

@seealso bigdecimal_intro, math_functions

Exercise: Practical.java

Calculate factorial of a large number and check if a number is prime