Recently looking at the collection of source code, found Arraydeque inside used a lot of & operations, which involves the binary system. Suddenly found themselves a bit fuzzy on the binary of negative numbers, and some support for this.
First of all we have a knowledge of the original code, anti-code and complement:
1, the so-called original code is the binary fixed-point notation, that is, the highest bit is the sign bit, "0" is positive, "1" is negative, the remaining bits represent the size of the value. 2, anti-code notation: the inverse code of the positive number is the same as its original code; the inverse of a negative number is a bitwise negation of its original code, except for the sign bit. The original code 10010 = anti-code 11101 (10010,1 is a character number, it is negative) (11101) binary = 130 3, the complement notation stipulates: The complement of positive numbers is the same as the original code, the complement of negative numbers in its inverse code and 1.
For example, let's look at how integer-1 is represented in a computer.
Assuming this is also an int type, then:
1, first take 1 of the original code: 00000000 00000000 00000000 00000001
2, the reverse code: 11111111 11111111 11111111 11111110
3. Complement: 11111111 11111111 11111111 11111111
Hexadecimal representation: 0xFFFFFFFF
So in Java, the result of integer.tobinarystring (-5) is 11111111111111111111111111111011. The integer is a 32-bit (bit).
Binary rules for negative numbers:
1, take the absolute value of the original code;
2. Calculate the inverse code of the original code;
3, add one to the anti-code, get the complement.
The source of the fall: http://blog.csdn.net/cl05300629
Analysis of negative-to-binary binary in Java