Comparing BigIntegers in Java

You can compare BigIntegers the same as you compare String or other objects in Java.

For example:

BigInteger one = BigInteger.valueOf(1);
BigInteger two = BigInteger.valueOf(2);

if(one.equals(two)){
      System.out.println("Equal");
}
else{
     System.out.println("Not Equal");
}

Output:

Not Equal

Note:

In general, do not use use the == operator to compare BigIntegers

  • == operator: compares references; i.e. whether two values refer to the same object
  • equals() method: compares the content of two BigIntegers.

For example, BigIntegers should not be compared in the following way:

if (firstBigInteger == secondBigInteger) {
// Only checks for reference equality, not content equality!
}

Doing so may lead to unexpected behavior, as the == operator only checks for reference equality. If both BigIntegers in java contain the same content but do not refer to the same object, this will fail. Instead, compare BigIntegers using the equals methods, as explained above.

You can also compare your BigInteger to constant values like 0,1,10.

for example:

BigInteger reallyBig = BigInteger.valueOf(1);
if(BigInteger.ONE.equals(reallyBig)){
     //code when they are equal.
}

You can also compare two BigIntegers by using compareTo() method, as following: compareTo() returns 3 values.

  • 0: When both are equal.
  • 1: When first is greater than the second (the one in brackets).
  • -1: When first is less than second.
BigInteger reallyBig = BigInteger.valueOf(10);
BigInteger reallyBig1 = BigInteger.valueOf(100);

if(reallyBig.compareTo(reallyBig1) == 0){
    //code when both are equal.
}
else if(reallyBig.compareTo(reallyBig1) == 1){
   //code when reallyBig is greater than reallyBig1.
}
else if(reallyBig.compareTo(reallyBig1) == -1){
   //code when reallyBig is less than reallyBig1.
}

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.

Binary Or:

BigInteger val1 = new BigInteger("10");
BigInteger val2 = new BigInteger("9");

val1.or(val2);

Output: 11 (which is equivalent to 10 | 9)

Binary And:

BigInteger val1 = new BigInteger("10");
BigInteger val2 = new BigInteger("9");

val1.and(val2);

Output: 8 (which is equivalent to 10 & 9)

Binary Xor:

BigInteger val1 = new BigInteger("10");
BigInteger val2 = new BigInteger("9");

val1.xor(val2);

Output: 3 (which is equivalent to 10 ^ 9)

RightShift:

BigInteger val1 = new BigInteger("10");
val1.shiftRight(1); // the argument be an Integer

Output: 5 (equivalent to 10 >> 1)

LeftShift:

BigInteger val1 = new BigInteger("10");
val1.shiftLeft(1); // here parameter should be Integer

Output: 20 (equivalent to 10 << 1)

Binary Inversion (Not) :

BigInteger val1 = new BigInteger("10");
val1.not();

Output: 5

NAND (And-Not):*

BigInteger val1 = new BigInteger("10");
BigInteger val2 = new BigInteger("9");

val1.andNot(val2);

Output: 7

Generating random BigIntegers

The BigInteger class has a constructor dedicated to generate random BigIntegers, given an instance of java.util.Random and an int that specifies how many bits will the BigInteger have. Its usage is quite simple – when you call the constructor BigInteger(int, Random) like this:

BigInteger randomBigInt = new BigInteger(bitCount, sourceOfRandomness);

then you’ll end up with a BigInteger whose value is between 0 (inclusive) and 2bitCount (exclusive).

This also means that new BigInteger(2147483647, sourceOfRandomness) may return all positive BigIntegers given enough time.

What will the sourceOfRandomness be is up to you. For example, a new Random() is good enough in most cases:

new BigInteger(32, new Random());

If you’re willing to give up speed for higher-quality random numbers, you can use a new
=”https://docs.oracle.com/javase/8/docs/api/java/security/SecureRandom.html” rel=”nofollow
noreferrer”>SecureRandom() instead:

import java.security.SecureRandom;

// somewhere in the code…
new BigInteger(32, new SecureRandom());

You can even implement an algorithm on-the-fly with an anonymous class! Note that rolling out your own RNG algorithm will end you up with low quality randomness, so always be sure to use an algorithm that is proven to be decent unless you want the resulting BigInteger(s) to be predictable.

new BigInteger(32, new Random() {
    int seed = 0;
    @Override
    protected int next(int bits) {
       seed = ((22695477 * seed) + 1) & 2147483647; // Values shamelessly stolen from

=”https://en.wikipedia.org/wiki/Linear_congruential_generator#Parameters_in_common_use” rel=”nofollow noreferrer”>Wikipedia return seed; } });

Leave a Comment