Original code true Code
The original code is very simple. A 10-digit positive number is the binary number converted by the Division 2 method.
For example, the source code of 8 is: 1000
Reverse Code complement, bitwise NOT
It is also very easy, that is, to flip every bit. For example:
NOT 0111 (decimal 7) = 1000 (decimal 8)
In C/C ++, use ~ Returns the inverse code.
One's Complement
This is a binary representation of a computer with signed integers. In short, positive numbers are the same, and negative numbers are reversed. The following example is from Wikipedia.
The table below shows all possible values in a 4-bit system, from −7 to + 7.
+ - 0 0000 1111 Note that +0 and −0 return TRUE when tested for zero, FALSE when tested for non-zero. 1 0001 1110 2 0010 1101 3 0011 1100 4 0100 1011 5 0101 1010 6 0110 1001 7 0111 1000
This method is not commonly used due to some shortcomings.
Two's Complement
This is the most common method for computers to represent signed integers.
If a positive number is not changed, if a negative number is encountered, the binary value is reversed to 1, and overflow is ignored.
The table below shows all possible values in a 4-bit system, from −7 to + 7.
+ - 0 0000 Note 0 has only one presentation:0000,no -0 1 0001 1111 2 0010 1110 3 0011 1101 4 0100 1100 5 0101 1011 6 0110 1010 7 0111 1001
This is the complement code.
Complement
The complement logic algorithm is: (positive numbers are the same, and the sign bit is 0)
1. The value of-7 (excluding symbols) is expressed as an integer of 111.
2. Add the symbol 1 to indicate a negative number, so it is 1111.
3. Only reverse the non-Signed bit, so it is 1000
4. Add 1, so 1001
Now let's see how the computer uses the two's Complement Algorithm to represent-7.
1.7 represents a bit of 00000111 (the number occupies at least one byte in the computer)
2. After the reverse is obtained, the result is 11111000.
3. After 1 is added, it becomes 11111001 (the high level is insufficient, and the computer uses the symbol bit to complement it)
The results are the same.