In-depth understanding of binary complement and further understanding of Complement

Source: Internet
Author: User

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)0The 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.

FormulaASumBWe can get a perceptual knowledge. We relaxed to a more general situation ,(XAndYIs 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 insideXAnd(-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 :(XPositive number)

Positive numberXThe completion code of is equalX(This can be understood as a positive number.XThe completion code of is equalX+K,K=0000)
Negative (-X).M-X+N,M=1111,N=0001;
Our problem is hereK,M,NCan it be changed to another value?

We try to modifyMAndN:M=1100,N=0100. We found thatMAndNForASumBNo 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-XThis 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=1111And then try to modifyN=0010That is, to modify the complement definition of a negative number to get anti-encryption.2. We found thatBThere 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 numberXToX+1).AIf 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 mapped1XXXIn the form of complement code, a total of 8 numbers. After this modification,-1The completion code of is changed0000, And0The complement code is also0000;7Actually1000. So not only is one-to-one ing broken, but the beginning of a negative numberbitIs1The complement form is also damaged!

If we keepM=1111, ModifyN=0000What about it? That is to say, modify the complement definition of a negative number to take the inverse. We found that1111And0000All0(In this case, either of them is required ),-8Then 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. SupposeXPositive number, original codeXIs definedX+K, Complement (-X) Is definedM-X+NIn this caseCIt 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 firstbitBit:1.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.