In-depth understanding of binary complement and further understanding of Complement
Article Source
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)0
The two types of complement codes 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.
FormulaA
SumB
We can get a perceptual knowledge. We relaxed to a more general situation ,(X
AndY
Is any positive integer ):
X-Y // C, which is used to reference (X) + (-Y) // The computer will add these two complement codes (if X> Y, A can push the = (X-Y) Complement code; if X <Y, by B can push = (-(Y-X) of the complement; the final sum, is = (X-Y) of the complement !) = (X-Y) of the complement code // No matter (X-Y) is positive or negative, are set up! Don't forget that the positive complement is the original code.
It can be seen that the computer is insideX
And(-Y)
Calculate the sum of the two complement CodesAnd the actual result is(X-Y)
Is exactly what we expected!
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 gossip :(X
Positive number)
Positive numberX
The completion code of is equalX
(This can be understood as a positive number.X
The completion code of is equalX+K
,K=0000
)
Negative (-X
).M-X+N
,M=1111
,N=0001
;
Our problem is hereK
,M
,N
Can it be changed to another value?
We try to modifyM
AndN
:M=1100
,N=0100
. We found thatM
AndN
ForA
SumB
No problem! Why not? In fact, the reason is very simple, but it is fatal: the circuit hardware implementation is reversed (the effect is1111-X
).1100-X
This form falls into the 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, we keepM=1111
And then try to modifyN=0010
That is, to modify the complement definition of a negative number to get anti-encryption.2
. We found thatB
There is no problem, but expression A is not satisfied! In fact, if we modify the positive complement code at the same time to define the original code plus1
(Positive numberX
ToX+1
).A
If you don't believe it, try it. In addition to modifying the positive complement definition, a new ing problem is introduced! What we expect is[-8,-1]
The entire interval can be mapped1XXX
In the form of complement code, a total of 8 numbers. After this modification,-1
The completion code of is changed0000
, And0
The complement code is also0000
;7
Actually1000
. So not only is one-to-one ing broken, but the beginning of a negative numberbit
Is1
The complement form is also damaged!
If we keepM=1111
, ModifyN=0000
What about it? That is to say, modify the complement definition of a negative number to take the inverse. We found that1111
And0000
All0
(In this case, either of them is required ),-8
Then there is no corresponding completion code. 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 it. SupposeX
Positive number, original codeX
Is definedX+K
, Complement (-X
) Is definedM-X+N
In this caseC
It 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 <Y, = M-(Y-X) + K + N. At this time, the analysis of the negative number (X-Y) of the complementary code and according to the negative number of the definition can be introduced: K + N = N, that is, K is 0) Finally we get M + N = 0, K = 0. Based on the fact that the hardware is easy to implement anti-code, M = 1111. So M = 1111N = 0001 K = 0000
Conclusion
Now we can review the code complementing mechanism. ActuallyThe hardware circuit is used to easily implement inverse subtraction.In implementation, the one-to-one ing principle is fully taken into account and the complement of negative numbers is the firstbit
Bit:1
.