The data is stored in a computer in accordance with the two's-saving concepts involved, what the original code in particular anti-complement code, will be described by the following examples.
positive number
A positive number of the original code, anti-code and complement the same. Positive original code, the highest bit position representing a symbol 1 stands for negative, 0 represents a positive number, see the specific examples.
int original code 20? You can quickly obtain the following results, 32 'bit, 0 is filled to high.
Original code: 00000000 00000000 0,000,000,000,010,100
According to the rules, and complement their anti follows.
Anti-code: 00000000 00000000 0,000,000,000,010,100
Complement: 00000000 00000000 0,000,000,000,010,100
negative number
Negative original code, the most significant bit is 1, the inverted most significant bit is fixed, the other becomes low 1 1 0,0 variants, complement plus 1 is based on the inverted, see specific examples.
int -20 of the original code? Can quickly obtain the following results, 32 'bit, except the most significant bit is 1, 0 is filled to the high other.
Original code: 1,000,000,000,000,000 0,000,000,000,010,100
Anti-code: 1,111,111,111,111,111 1,111,111,111,101,011
Complement: 1,111,111,111,111,111 1,111,111,111,101,100
0
Taking into account the high is 0, other bits are all zero can be expressed as +0, 1 is high, there are other bits are all 0 can be expressed as -0, so there are two zeros in the computer -0 represents the current data minimum type.
. 1 public class operatorDemo { 2 . 3 public static void main (String [] args) { . 4 // test increment-decrement type conversion . 5 byte B = 127 ; . 6 // B = +. 1; // write No problem 7 // = B +. 1 B; // write the compiler does not pass . 8 B ++; // write no problem . 9 System.out.println (B); 10 } . 11 12 is }
The output is -128, because the byte 127 is the complement 01111111 is performed by calculating from the first is an int i.e. 00000000 00000000 01111111, then add 1 becomes 00000000 00000000 0,000,000,010,000,000, and finally down to high transition discarded byte becomes 10 million, which is -0, found that this can not be presumed anti-code complement the original code again, so artificial and -128, and -128 to deal with in accordance with the computer deal with this complement. If a byte complement is 1111 1111, it should be? This high level of 1 is a look at the negative, you can easily launch anti-code 1111 1110, to re-introduce the original code is 1000 0001, it represents the original code is -1, can be seen complement 1111 1111 - 1000 0000 represents the range of - 1 ~ -128 negative.
Reference Hirofumi: