BigInteger in java

In this post, we will see about BigInteger in java.

Why do we need BigInteger?

Java provides various primitive data types such as int, long , and double etc.but these data types can not handle very large numbers and will overflow causing issues in program.

BigInteger is introduced in java to handle very large integers and this class provides all the java primitive integer operators and required methods of java.lang.Math. BigInteger can handle very large integers and only limited by the available memory in JVM.

Let me demonstrate this with the help of an example.

As you can see, I have written two versions of the factorial calculation. factorial() returns long whereas factorialBigInteger() returns BigInteger. We will run the above program to calculate factorial of 40 with the help of both methods.

When you run the program, you will get below out:

Factorial of 40 (Long): 0
Factorial of 40 (BigInteger): 815915283247897734345611269596115894272000000000

As you can clearly see, BigInteger was able to handle factorial of 40, whereas long did not give correct result due to overflow issue.

Should we always use BigInteger instead of Integer?

BigInteger should be only used whenever required, as there is performance hit associated with BigInteger and also memory taken per BigInteger is relatively very high compared to built-in types.

BigInteger is an immutable arbitrary-precision integer.

Initialize BigInteger objects

You can create BigInteger from a byte array or String.
Let’s see with the help of an example.

You can also convert other radix String to BigInteger using constructor BigInteger(String val, int radix)

Output:

BigInteger for hexaDecimal 28A: 650

Compare two BigInteger objects

We can use compareTo() method to compare two BigIntegers. BigInteger returns 0, 1 or -1.

Let’s see with the help of an example:

Output:

comparing b1 and b2: -1
comparing b1 and b3: 0
comparing b2 and b3: 1

You can also use equals method also in case you want to check BigInteger equality. Just change compareTo to equals in above example

Output:

comparing b1 and b2: false
comparing b1 and b3: true
comparing b2 and b3: false

BigInteger class provides constants for ZERO, ONE and TEN, so in case you want to compare it with these 3 constants, you need not create new objects.

Output:

comparing b1 to 0: true
comparing b2 to 1: true
comparing b3 to 10: true

Bit operations

BigInteger has bit operations similar to int and long. We need to use methods instead of operations in case of BigInteger.

Output:

Binary presentation of b1: 1100100
b1 and b2: 64
b1 or b2: 236
b1 xor b2: 172
b1 not: -101
b1 andNot b2: 36
b1 shiftLeft: 200
b1 shitfRight: 50
b1 bitcount: 3
b1 bitLength: 7
b1 getLowestSetBit: 2
b1 testBit2: true
b1 setBit3: 108
b1 flipBit3: 108
b1 clearBit2: 96

Min, Max, pow, abs, signum operations

BigInteger provides methods to perform various operations such as Min, Max, pow, abs, signum.
Let’s see with the help of an example.

Modular and GCD computation

BigInteger provides in-built methods for modular and GCD calculations.
Here is an example of the same.

Output:

GCD b1 and b2: 5
mod b1 and b3: 3

Prime generation and primality testing

BigInteger also has methods for Prime generation and primality testing.

Output:

probable prime: 541

Conclusion

In this article, you learned about Why should we use BigInteger, how to initialize BigInteger, and do various operations such as min, max, signum, pow, gcd, mod, and also bit operations.

That’s all about BigInteger in java.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *