Whats is Java BigInteger?
The class BigInteger is in the package java.math
In Java, we have 4 data types to deal with integer type data. Those are byte, short, int and long.
- With byte, we can store numbers up to 127.
- With short, we can store numbers up to 32767.
- With int, we can store numbers up to 2147483647.
- With long, we can store numbers up to 9223372036854775807.
So for most of our integer calculations, these data types are good enough.
Sometimes, we may need to work with integers with 20 or more digits. In that case, these existing data types are not sufficient. BigInteger is the answer in these situations.
BigInteger Class In Java
We have many constructors for this class. One of the simple ones will take a String as an argument where the string is treated as integer internally. There is no limit on the size (number of digits) of the number.
Example:
1 |
BigInteger big=new BigInteger(“13102009170119870210197804092016”); |
We cannot use the operators like + and – directly on the BigInteger objects. But we have methods to perform add, subtract, etc on these objects.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
import java.math.*; class Check { public static void main(String arg[]) { BigInteger n1=new BigInteger("94"); BigInteger n2=new BigInteger("12"); System.out.println( n1+n2 ); // invalid System.out.println( n1.add(n2) ); // prints 106 System.out.println( n1.subtract(n2) ); // prints 82 System.out.println( n1.multiply(n2) ); // prints 1128 System.out.println( n1.divide(n2) ); // prints 7 System.out.println( n1.remainder(n2) ); // prints 10 } } |
To perform relational operations on BigIntegers, we can use methods like equals() and compareTo(). We know equals() method returns true or false and compareTo() method returns -1 or 0 or 1.
The valueOf() is a static method in BigInteger that takes a normal integer as argument and returns its equivalent BigInteger object.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
BigInteger n1=BigInteger.valueOf(24); BigInteger n2=BigInteger.valueOf(12); BigInteger n3=BigInteger.valueOf(24); System.out.println( n1.equals(n2) ); // false System.out.println( n1.equals(n3) ); // true System.out.println( n1.compareTo(n2) ); // 1 System.out.println( n1.compareTo(n3) ); // 0 System.out.println( n2.compareTo(n1) ); // -1 |
In this example, we have taken smaller values for simplicity. But there is no limit on its size. The class has some mathematical methods like pow() and gcd() that calculate Power-Of and Greatest-Common-Divisors.
Similarly, we have methods to perform bitwise operations (and, or, xor, not, etc) also on the values. All the following mentioned methods return a BigInteger object.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
BigInteger n1=new BigInteger("6"); BigInteger n2=new BigInteger("12"); System.out.println( n1.pow(3) ); // prints 216 System.out.println( n1.gcd(n2) ); // prints 6 System.out.println( n1.and(n2) ); // prints 4 System.out.println( n1.or(n2) ); // prints 14 System.out.println( n1.xor(n2) ); // prints 10 System.out.println( n1.not() ); // prints -7 |
Points To Note
Internally the values are stored in normal 2’s complement notation only (generally, the integers in Java are also stored in this format only).
On BigIntegers also, a division by 0 results in ArithmeticException.
Some more constructors:
BigInteger(byte[]):
This constructor takes a byte array and forms a number by appending all the bits of each byte (as each value is stored as an 8-bit sequence).
Example:
1 2 3 |
byte b[]={4,8}; BigInteger b1=new BigInteger(b); |
In this example, the values in the array are 4 and 8. If we place bit sequences, those will look like
00000100 00001000
When all the bit sequence is considered as a single number it becomes 1032 (1024+8)
BigInteger(String, int)
This method takes a String as the first argument which consists of a digit sequence and an integer as the second argument that represents radix of the specified String.
It means, if the system should treat the String as a binary number (set of bits in binary number system) then the second argument should be 2.
If the system should treat the string as a hexadecimal number then the second argument should be 16.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
BigInteger b1=new BigInteger("10101",2); BigInteger b2=new BigInteger("111001",2); BigInteger b3=new BigInteger("1234",10); BigInteger b4=new BigInteger("1234",16); BigInteger b5=new BigInteger("2A3",16); System.out.println(b1); // 21 [ 10101(2) is 21(10) ] System.out.println(b2); // 57 [ 111001(2) is 57(10) ] System.out.println(b3); // 1234 [ 1234(10) is 1234(10) ] System.out.println(b4); // 4660 [ 1234(16) is 4660(10) ] System.out.println(b5); // 675 [ 2A3(16) is 675(10) ] |