-
Can you find any overflow when you walk along the line ?!
The representation of binary data includes the original code, reverse code, and complement code.
The original code is the representation of a data, which has not been modified. For example, [0001 1011] (27), [1001 1011] (-101)
The reverse code is the result of bitwise inversion of the original code. The original code of a positive number is the original code itself. The reverse code of a negative number can be obtained by the highest sign bit of the original code, and the rest by bit. Found, the reverse code can represent signed integers, from-127 ~ + 127, but [0000 0000] (+ 0) and [1111 1111] (−0) are displayed ). The complement code can fill in the anti-code deficiency. Interestingly, the unsigned addition and subtraction operations can be implemented only through addition through the reverse code.For the four-digit binary number, X ++ ~ X + 1 = 2 ^ 4, obviously [0011] + [1100] + 1 = [1 0000]. This conclusion is very important.
There is no need to explain the addition, that is, the modulus 2 ^ 4 addition, but how the subtraction is transformed can be implemented through addition. For example, the unsigned integer subtraction is 6-11.
0 ---------------------> <------ 15
"4-7" means that an ant will take seven steps in the negative direction of coordinate 4, but it will overflow. Therefore, the tail end 15 of the returned coordinate continues to walk in the negative direction, this process is the same as that of the ant in coordinate 4 in the forward direction (16-7), and (16-7) is exactly equal (~ 7 + 1 ). So 4-7 = 4 + ~ 7 + 1 = 14. Therefore, an addition or subtraction operator can convert the negative walking (subtraction) of ant financial into positive walking (addition ). You will find that this is the complement addition.
Complete the code. Positive and 0 are their own supplementary codes, while negative complement codes are the highest sign bit unchanged, and the rest are reversed by 1, for example, [0001 1011] Complement code for itself, [1001 1011] the supplemental code is [1110 0100]. Interestingly, in the complement representation, in the binary representation of the W Bit, the highest valid bit is interpreted as negative weight, the weight is correspondingly negative, for-2 ^ (W-1 ). For example
- [0101 0001] =-0*2 ^ 7 + 1*2 ^ 6 + 0*2 ^ 5 + 1*2 ^ 4 + 0*2 ^ 3 + 0*2 ^ 2 + 0*2 ^ 1 + 1*2 ^ 0 =-0 + 64 + 0 + 16 + 0 + 0 + 0 + 1 = 81
- [1101 0001] =-1*2 ^ 7 + 1*2 ^ 6 + 0*2 ^ 5 + 1*2 ^ 4 + 0*2 ^ 3 + 0*2 ^ 2 + 0*2 ^ 1 + 1*2 ^ 0 =-128 + 64 + 0 + 16 + 0 + 0 + 0 + 1 = 47
As mentioned above, the complement code avoids the deficiency of the reverse code: There are two 0 representation. In the complement code, 0 only indicates [0000 0000], and [1000 0000 128] only indicates the minimum negative number-due to negative weight, [0111 1111] That is, the maximum positive number is 127. The value range of the complement code is-128 ~ 127.
To4-bit binary operation4-7 is used as an example to show whether there is a negative number. For a positive number, the signed and unsigned plan is obviously the same. The four-digit binary signed integer-7 is 9 of the four-digit binary unsigned integer. There are
- Unsigned operation:4-7 = 4 + (-7) = [0100] + ~ [0111] + 1 = [1101] = 13 (use the above anti-code method)
- Signed operation:4-7 = 4 + (-7) = [0100] + [1001] = [1101] =-3 (add with complement code)
It is found that the calculation process is the same, and the results are essentially the same, but the machine has different interpretations of the signed and unsigned, therefore, most computers use the same machine command to execute an unsigned or signed addition (subtraction can be performed on behalf of addition ).
-7 and 9 are the same, more about the same concept: the same formula.
- Limit 4 limit 4 (mod 16)
- Again-7 then 9 (mod 16)
- Based on the Equality formula, we can obtain:-7 + 4 + 9 + 4 (mod 16)
-
Addition, subtraction, and multiplication are maintained in the same formula.
Since the results for mode 16, 4-7, and 4 + 9 are the same, why is the CPU designed to only have the latter (addition?
Similarly, for other situations:
-1 limit 15 (mod 16)
-2 limit 14 (mod 16)
-3 Route 13 (mod 16)
...
Another example:
- Unsigned operation:1-5 = [0001] + [1011] = [1100] = 12 (for the sake of intuition, the above reverse code conversion is omitted) Another example:
- Signed operation:1-5 = [0001] + [1011] = [1100] =-4 (the red part is the actual operation of the divider)
Return to the example of ant financial. In the signed negative direction, go to Step 5-5, and change to Step 11. Subtraction is eliminated. When we take a course on computer composition principle, we often recite it. If we subtract a number, it is equal to the Supplementary Code of this number. The occurrence of the complement operation integrates the unsigned and signed addition and subtraction operation. No matter how the machine interprets the binary number, the work of the calculator is the same. Of course, there will be positive overflow or negative overflow in signed or unsigned operations. In this case, we need to perform the 2 ^ w operation (W: binary digits ). I 've always wanted to write this article, but I don't know if I can tell it clearly. So I 've put it on hold for a long time. If you have a better idea about the computer's original code, back code, and complement code, don't hesitate.
Daoluan.net