Use BigInteger to process large binary numbers

Source: Internet
Author: User

 

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.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.