JVM principle integer Representation in Java Virtual machine

Source: Internet
Author: User

In a Java virtual machine, integers have byte,short,int,long four species, 8-bit, 16-bit, 32-bit, and 64-bit signed integers. Integers are expressed in the computer using the complement, in the Java Virtual machine is also in the form of complement;

The complement definition: the complement of the negative number is the inverse code plus 1, the integer's inverse code is its original code;

What is the original code: the original code is the symbol bit plus the binary representation of the number.

Take int as an example: the 1th bit denotes a sign bit (positive or negative), and the remaining 31 digits represent the binary value of the number.

10 The original code: 00000000000000000000000000001010

-10 of the original code: 10000000000000000000000000001010

For the original code, positive and negative numbers with the same absolute value have only different sign bits.

The inverse code is based on the original code, the symbol bit unchanged, the rest of the reverse, to-10 for example, its inverse code is

11111111 11111111 11111111 11110101

The complement of the negative number is the inverse code plus 1, and the complement of the integer is the original code itself.

Therefore, the 10 complement is:

00000000000000000000000000001010

And-10 's complement is:

11111111 11111111 11111111 11110110

In Java, you can use bitwise operations to view the actual values of each digit in an integer by using the following methods:

int a =-10;

for (int i=0;i<32;i++) {

int t= (a&0x80000000>>>i) >>> (31-i);

System.out.printl (t);

}

The code runs the result:

11111111 11111111 11111111 11110110

There are two of them in the complement:

1. Harmonization of the representation of the number 0;

2. Simplify the calculation of integer addition and subtraction, the subtraction is considered as addition calculation, realize the complete unification of subtraction and addition, realize the unification of positive and negative addition.



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.