Chapters
BigInteger class is immutable arbitrary-precision integers. This class is counterpart of int primitive type. Although, Unlike int primitive, BigInteger can store a very large number that suprasses even the long value range. BigInteger has a range value of -2^Integer.MAX_VALUE(exclusive) to 2^Integer.MAX_VALUE(exclusive) and may support values outside of that range.
By the way,
BigInteger has several constructors. Although, I'm only gonna demonstrate two of them which are easy to understand and implement. Other constructors can be seen in the documentation.
This constructor
Since BigInteger is an object type, standard operators like "+" operator don't work with BigInteger. However, BigInteger has methods that are analogous to standard operations. I'm gonna demonstrate some of them here.
BigInteger has methods that will let us convert the value in BigInteger to primitive types. We already know that we can convert BigInteger value to string by using
Note: Typecasting principles are still applied when converting BigInteger value to a primitive type.
This method negates BigInteger value. We use this method to make a BigInteger negative if the BigInteger value is positive or positive if the BigInteger value is negative.
BigInteger Class
BigInteger class is immutable arbitrary-precision integers. This class is counterpart of int primitive type. Although, Unlike int primitive, BigInteger can store a very large number that suprasses even the long value range. BigInteger has a range value of -2^Integer.MAX_VALUE(exclusive) to 2^Integer.MAX_VALUE(exclusive) and may support values outside of that range.
By the way,
Integer.MAX_VALUE = 2,147,483,647.
BigInteger Constructors
BigInteger has several constructors. Although, I'm only gonna demonstrate two of them which are easy to understand and implement. Other constructors can be seen in the documentation.
import java.math.BigInteger;
public class SampleClass{
public static void main(String[] args){
//1 byte = 8 bits
byte[] bytes = {0b0101110, 0b00001010};
//Constructor form
//public BigInteger(byte[] val)
BigInteger bi1 = new BigInteger(bytes);
//use toString() to get the BigInteger
//value in String form
System.out.println("output1: " + bi1.toString());
//Constructor form
//BigInteger(String val)
BigInteger bi2 = new BigInteger("1500000000");
System.out.println("output2: " + bi2.toString());
//Constructor form
//BigInteger(int signum, byte[] magnitude)
BigInteger bi3 = new BigInteger(-1, bytes);
System.out.println("output3: " + bi3);
}
}
Result
output1: 11786
output2: 1500000000
output3: -11786
This constructor public BigInteger(byte[] val) accepts an array of bytes then, the bits of the bytes in the array are concatenated. This constructor BigInteger(String val) converts the string to a BigInteger digit.
This constructor
BigInteger(int signum, byte[] magnitude) accepts and array of bytes, the bits of the bytes in the array are concatenated and translates the sign represented as an integer signum value: -1 for negative, 0 for zero, or 1 for positive. This constructor throws NumberFormatException if signum is not one of the three legal values (-1, 0, and 1), or signum is 0 and magnitude contains one or more non-zero bytes.
BigInteger Operations
Since BigInteger is an object type, standard operators like "+" operator don't work with BigInteger. However, BigInteger has methods that are analogous to standard operations. I'm gonna demonstrate some of them here.
import java.math.BigInteger;
public class SampleClass{
public static void main(String[] args){
BigInteger bi1 = new BigInteger("1040700980");
BigInteger bi2 = new BigInteger("2500555000");
BigInteger result = null;
//add two BigInteger values
result = bi1.add(bi2);
System.out.println(bi1 + " + " + bi2 + " = " + result);
//subtract two BigInteger values
result = bi2.subtract(bi1);
System.out.println(bi2 + " - " + bi1 + " = " + result);
//multiply two BigInteger values
result = bi1.multiply(bi2);
System.out.println(bi1 + " * " + bi2 + " = " + result);
//divide two BigInteger values
result = bi2.divide(bi1);
System.out.println(bi2 + " / " + bi1 + " = " + result);
}
}
Result
1040700980 + 2500555000 = 3541255980
2500555000 - 1040700980 = 1459854020
1040700980 * 2500555000 = 2602330039043900000
2500555000 / 1040700980 = 2
Converting BigInteger value to Primitive Type
BigInteger has methods that will let us convert the value in BigInteger to primitive types. We already know that we can convert BigInteger value to string by using
toString() method. In this topic, I'm gonna demonstrate converting BigInteger value to primitive types.Note: Typecasting principles are still applied when converting BigInteger value to a primitive type.
import java.math.BigInteger;
public class SampleClass{
public static void main(String[] args){
BigInteger bi = new BigInteger("12345678");
//intValue() converts BigInteger to
//int primitive
int intVal = bi.intValue();
System.out.println("int: " + intVal);
//longValue() converts BigInteger to
//long primitive
long longVal = bi.longValue();
System.out.println("long: " + longVal);
//shortValue() converts BigInteger to
//short primitive
short shortVal = bi.shortValue();
System.out.println("short: " + shortVal);
}
}
Result
int: 12345678
long: 12345678
short: 24910
negate() Method
This method negates BigInteger value. We use this method to make a BigInteger negative if the BigInteger value is positive or positive if the BigInteger value is negative.
import java.math.BigInteger;
public class SampleClass{
public static void main(String[] args){
BigInteger bi1 = new BigInteger("-1040700980");
BigInteger bi2 = new BigInteger("2500555000");
BigInteger result = null;
//negate bi2.
bi2 = bi2.negate();
result = bi2.add(bi1);
System.out.println(bi2 + " + " + bi1 + " = " + result);
//negate bi2 again. Now bi2 sign is positive again.
bi2 = bi2.negate();
result = bi2.add(bi1);
System.out.println(bi2 + " + " + bi1 + " = " + result);
}
}
Result
-2500555000 + -1040700980 = -3541255980
2500555000 + -1040700980 = 1459854020
No comments:
Post a Comment