Article source

Background

We all know that the internal use of the computer complement to express the integer, but the specific to the inner meaning of the complement, many people do not understand, so we share their understanding.

First, the definition and the basic nature of the complement:

1) The complement of positive number is the same as the original code;

2) The complement of negative is equal to the inverse plus 1;

3) `0`

the positive and negative two kinds of complement are the same;

4) to a complement and then seek the complement equals oneself;

5) A positive number of the original code and its corresponding negative number of the complement of the sum equal to the modulus;

For this article, we're actually only interested in Rule 1) and 2).

Instance

For the convenience of explaining the existing complement definition (that is, the positive complement equals the original code; the complement of the negative number equals the inverse plus 1), this article takes the eight binary as an example.

`5 – 3 //记作式A，方便后文引用= 5 + ( -3 )（下面开始转为二进制补码格式）= 0101 + ( 1111 – 0011 + 1 ) //展开-3的补码形式= ( 0101 – 0011 ) + ( 1111 + 0001 ) //交换= ( 0101 – 0011 ) + ( 0000 ) //此处10000溢出为0000= 0010 //得出结果是2的补码，也是2的原码`

`2 – 3 //记作式B，方便后文引用= 2 + ( -3 )（下面开始转为二进制补码格式）= 0010 + ( 1111 – 0011 + 0001 ) //展开-3的补码形式= 1111 – ( 0011 – 0010 ) + 0001 //交换= 1111 – 0001 + 0001= 1111 //得出结果是-1的补码`

By Formula `A`

and Formula `B`

, we can get a perceptual knowledge. We relax to a more general situation ( `X`

and `Y`

for any positive integer):

`X – Y //记作式C，方便后文引用= (X的补码) + ((-Y)的补码) //计算机将这样的两个补码将加（如果X>Y，由式A可以推得=(X – Y )的补码；如果X<Y，由式B可以推得=(-(Y-X))的补码；最终归结，就是=(X-Y)的补码！）= (X-Y)的补码 //不管(X-Y)是正还是负，都成立！别忘了正数的补码就是原码。`

Visible, the computer inside is in pairs `X`

and calculates the sum of `(-Y)`

**two complement** , and the actual result is `(X-Y)`

the complement, exactly what we expect!

At this point, we know that the existing complement mechanism is indeed working properly!

Extended

The complement definition we used above is well known (that is, the positive complement equals the original code; the complement of the negative is equal to the inverse plus 1). So our question is, can the complement definition be modified?

We look at the complement definition for octal specifically: ( `X`

positive)

A positive `X`

complement is equal to `X`

the original code; (This is understood here as a positive `X`

complement equals `X+K`

, `K=0000`

)

`-X`

the complement of negative () is equal `M-X+N`

to, `M=1111`

`N=0001`

;

Our problem is here `K`

, `M`

and can be `N`

modified into other values?

We try to modify `M`

and `N`

: `M=1100`

, `N=0100`

. We found that this and the formula `M`

`N`

and the `A`

formula `B`

is no problem! So why not use it? In fact, the reason is very simple, but very lethal: the circuit hardware implementation of the reverse (the effect is `1111-X`

) very easy, but the implementation `1100-X`

of this form is again into the subtraction calculation. After all, we are **hoping that subtraction can also be done with Adder** , which is why we use the introduction of complementary code integers!

Well, let's keep it, and `M=1111`

then try to modify `N=0010`

it, that is, change the complement definition of a negative number to take the inverse plus `2`

. We found that `B`

there is no problem with the formula, but the formula A is not satisfied! In fact, if at this time we also modify the complement of the positive number is defined as the original code plus `1`

(positive `X`

complement modified to `X+1`

), you can meet the formula `A`

, do not believe, please try. In addition to modifying the positive complement definition, it also introduces a new problem of mapping! We expect that the `[-8,-1]`

entire range can be mapped to `1XXX`

the form of the complement, a total of 8 numbers. And we have so modified, `-1`

the complement has become `0000`

, and `0`

the complement is also, `0000`

`7`

the complement is instead `1000`

. So not only the mapping is destroyed, the first of the negative number of `bit`

`1`

the complement form also destroyed!

What if we keep it `M=1111`

, change `N=0000`

it? That is, the complement definition of a negative number is changed to just negation. We find that the `1111`

`0000`

`0`

complement of the present and the same is indicated (at which point any one can be specified), and `-8`

there is no corresponding complement. Visible, at this time only wasted a complement expression (-8) only. In fact, some manufacturers have done so in the past. If you look at the C language standard, you can see that the waste is actually wasted (8 for our example) is allowed.

Now it can be deduced that, assuming a `X`

positive number, the definition of the original code is, the `X`

`X+K`

complement ( `-X`

) is defined as `M-X+N`

, this time the formula `C`

becomes:

`X - Y //记作式D，方便后文引用= X + (-Y)（下面开始转为二进制补码格式）= X + K + ( M - Y + N )+ M ++ M + N = K，即M + N = 0。如果X<Y，= M - (Y-X)+ K + N。此时分析负数(X-Y)的补码并根据负数补码定义可以推出：K+N=N，即K为0）最终我们得出M + N = 0，K = 0。而根据硬件易于实现反码的事实，M = 1111。故M = 1111N = 0001K = 0000`

Conclusion

Now we can re-examine the complement mechanism. In fact, the **use of hardware circuit is easy to implement the inverse** of the implementation of the subtraction, in the implementation of the full consideration of the one by one mapping principle and to ensure that the complement of negative numbers is the first `bit`

`1`

form.

In-depth understanding of twos complement