Deep understanding of binary Complement
Background
We all know that the computer uses a complement to represent an integer, but many people cannot understand the meaning of the complement, so we can share our understanding.
First, let's talk about the definition and basic nature of the complement code:
1) The positive complement is the same as the original code;
2) the complement of a negative number is equal to 1 after obtaining the reversed value;
3) The positive and negative complement codes of 0 are the same;
4) calculate a complement code to be the same as yourself;
5) the complement of a positive original code and its corresponding negative number is equal to the modulo;
For this article, we only care about rules 1 and 2.
Instance
To help describe the existing complement definition (that is, the positive complement is equal to the original code; the negative complement is equal to the inverse plus 1), this article uses octal as an example.
5-3 // expression A, which is convenient for subsequent reference = 5 + (-3) (The following is converted to the binary complement format) = 0101 + (1111-0011 + 1) // expand-3's complement form = (0101-0011) + (1111 + 0001) // exchange = (0101-0011) + (0000) // here 10000 overflow is 0000 = 0010 // The result is a complement of 2, which is also the source code of 2
2-3 // record B, which is convenient for later reference = 2 + (-3) (The following is converted to the binary complement format) = 0010 + (1111-0011 + 0001) // expand-3's complement form = 1111-(0011-0010) + 0001 // exchange = 1111-0001 + 0001 = 1111 // The result is-1's complement.
By expression A and B, we can get A perceptual knowledge. Let's relax to a more general situation (X and Y are any positive integers ):
X-Y // C, which is used to reference (X) + (-Y) // The computer will add two such supplementary codes (if X> Y, the complement code of formula A can be pushed = (X-Y); If X It can be seen that the computer is inside X and (-Y)Calculate the sum of the two complement CodesAnd the actual result is (X-Y) complement, is what we expect!
At this point, we know that the current Code complement mechanism is indeed working normally!
Extension The complement definition we use above is well-known (that is, positive complement equals the original code; negative complement equals to anti-increment 1 ). So our question is, can the definition of the supplemental code be modified? Let's take a look at the bucket definition for octal: (X is a positive number)
Positive X is equal to the original code of X; (here we can understand that positive X is equal to X + K, K = 0000) negative (-X) is equal to M-X + N, M = 1111, N = 0001; our question is, can K, M, and N be changed to another value?
We try to modify M and N: M = 1100, N = 0100. We found that M and N are okay for type A and type B! Why not? In fact, the reason is very simple, but it is fatal: the circuit hardware implementation is very easy (the effect is 1111-X), but the implementation of 1100-X is in the form of subtraction calculation. After all, our original intention isIt is hoped that the subtraction can also be completed using the addator.This is also the reason why we use the Supplementary Code to encode integers!
Okay, let's keep M = 1111, and then try to modify N = 0010, that is, to modify the complement definition of negative numbers to get and add 2. We found that there is no problem with type B, but type A is not satisfied! In fact, if we change the positive value of the complement code to the original code plus 1 (the positive number of X is changed to X + 1) at the same time, the expression A can be satisfied. If you do not believe it, try again. In addition to modifying the positive complement definition, a new ing problem is introduced! We expect that [-8,-1] the entire range can be mapped to the complement code in the form of 1XXX, a total of 8 numbers. After this modification, the-1 code is changed to 0000, And the 0 code is also 0000. The 7 code is 1000. So not only is one-to-one ing damaged, but the complement form with the first bit of the negative number being 1 is also damaged!
What if we keep M = 1111 and modify N = 0000? That is to say, modify the complement definition of a negative number to take the inverse. We found that at this time, both 1111 and 0000 are 0's complement representation (this requires any one), and-8 does not have the corresponding complement. It can be seen that at this time, only a supplementary code representation (-8) is wasted. In fact, in the past, some manufacturers achieved this. If you look at the C language standard, you can see that waste (for our example,-8) is allowed.
Now we can deduce, assuming that X is a positive number, the original code X is defined as X + K, the complement code (-X) is defined as M-X + N, then the formula C becomes:
X-Y // indicates the expression D, which is convenient for subsequent reference = X + (-Y) (The following is converted to the binary complement format) = X + K + (M-Y + N) (if X> Y, = K + M + N + (X-Y ). At this time, the analysis of positive number (X-Y) of the completion code and according to the positive number of completion Code definition can be introduced: K + M + N = K, that is, M + N = 0. If X
Conclusion
Now we can review the code complementing mechanism. ActuallyThe hardware circuit is used to easily implement inverse subtraction.In implementation, we fully consider the one-to-one ing principle and ensure that the complement code of the negative number is in the form of the first bit as 1.