In a program, bit logic usually needs to shift the left or right of a binary number, and also needs to perform bitwise operations on it to determine which bit values are set. If developers only need to handleThe binary number below 64 bits. They only need to use the long or int type.
If developers need to process binary data that exceeds 64 bits, they will need to use the java. math. BigInteger class. (BigInteger is an integral part of the core API after JDK 1.1 .)
BigInteger type. It has several constructor methods, but the most direct one is to use a java. lang. String type object to represent the number to be processed by the developer. For example:
BigInteger bi = new BigInteger
("Fffffffffffff", 16 );
In the code above, the BigInteger instance is created by a BigInteger constructor that uses the String and integer types as parameters. The value of the integer type is used to represent the base number of The number.
Once an instance is created, you can perform the operations provided by JDK on it. Similar to the java. lang. String type, the BigInteger class remains unchanged. Therefore, any operation on the instance will return a new instance. The value of this instance is the result of the operation.
There are some basic arithmetic operations in the BigInteger class, such as addition, subtraction, multiplication, division, and bit operations that may be used, such as or, XOR, non, left shift, and right shift. The following is an example of some methods:
BigInteger result = bi. multiply (new BigInteger ("2 "));
System. out. println (result );
Result = bi. divide (new BigInteger ("2 "));
System. out. println (result );
Result = bi. add (new bigint ("232 "));
System. out. println (result );
Result = bi. subtract (new bigint ("23122 "));
System. out. println (result );
Result = bi. shiftRight (10 );
System. out. println (result );
In the BigInteger class, other constructor methods and quick and efficient methods make it easier for developers to process large numbers.