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.