The original code, anti-code and complement (reprint)

Excerpt: https: //blog.csdn.net/zhiwen_a/article/details/81192087
for to facilitate learning

In this paper we start from the original code. Brief by the original code, and complement the presence of anti-effects, better understanding of complement. And strive to allow you to complement the concept is no longer limited to: a negative complement equal to the inverted plus one.

Contact the computer or electronic information related courses students should have seen more or less complement this brother Sa. Each time in a few pages before most textbooks, to the so: What is the original code in addition to the inverted sign bit, Bitwise. It is equal to the inverted complement plus one. Then to get the whole somehow, curiously, it followed by page, behind the content anyway, not much to do with three yards.

I had also read it several times did not understand. An old saying: Shibuguosan. When I learn C language, read once. Do not understand? Look, when "basic computer composition principle" of read, or do not know! Junior year, the "single-chip microcomputer principle and interface technology," I still do not understand. To the end of the period, when the review, and the quarters of the people Xialiao. Say something about these codes Yeah, I said I was not very clear way. And then say how demand side yard, side count. Playing playing, suddenly I understand. I said well, come to a halt. Do not say, I leave a good idea in order, so there is this amount. . Discussion thread count it.

alright, do not piffle any more. We started the original code, anti-code complement journey.

(A) Preliminaries

Know binary, hexadecimal. Interconversion of operation will be binary and decimal

Determined by the hardware of the computer, any data stored in the computer, which are essentially binary code storage.

The frame structure of a classical computer ~ von Neumann-out system. A computer by the operator, controller, memory, input and output devices. Where the operator, only an adder , not subtraction operator (It is said that a beginning is there, and later as a subtractor hardware cost too much, was abolished)

Therefore, the computer can not do the direct subtraction, subtraction it is achieved by the addition . You might say, in the real world all the subtraction can be as additions, minus a number, can be seen as the opposite of adding this number. Of course yes, but the prerequisite is to have the concept of negative numbers. That is why the sign bit had to introduce a damn.

And from a hardware point of view, only be considered positive plus negative subtraction .
And adding a positive number of positive, negative and negative adding, in fact, it can be directly summed by an adder.
The original code, anti-code generation process complement, is to solve, and the introduction of computer do the subtraction sign bit (plus and minus) problems.

This article is rather long, no need to read all of a sudden. The original code, anti-code, complement, read by chapter.
Emphasis speak complement, to complement may be some around, it is recommended with a pen, write binary number with the operator.

The expression may not be clear rigorous, hope forgive me.

(B) the original code

Original code: is the simplest representation of the number of machines. Symbol bits represented by the most significant bit, '1' denotes a negative number, '0' denotes a positive number . Other bits to store the absolute value of the binary number.

In terms of number into four-bit signed binary Example

1010: the most significant bit to '1', indicates that this is a negative number, the other three as '010',
i.e., (0 2 2) + (1 2 * 1) + (0 2 0) = 2 ( ' ' represents exponentiation character)
so that 1010 represents the decimal number (-2).
The following figure shows the number of positive and negative partial binary number representation of the original code
Here Insert Picture Description

OK, the original code is very simple representation there, despite a ** ** + 0 and -0, but intuitive.
So, we are pleased to begin operations.

0011 + 0010 = 0001 (= 2 + 1. 3) the OK
0000 + 1000 = 1000 (+0 + (- 0) = - 0) amount, the problem is not
0001 + 1001 = 1010 (1 + (- 1) = - 2 )
Oh, 1 + (- 1) = - 2, as it were teasing me.

So we can actually see the addition between positive number usually can not go wrong, because it is a very simple binary addition.

Adding the positive and negative, or negative and negative sum, the results must somehow induced, which is due to the sign bit damn. 0 divided +0 -0 and also because of his sky.

Therefore the original code, although intuitive, easy-to-positive conversion. But to achieve the addition and subtraction of words, rules of operation is always too complex. So the anti-code.

(C) anti-code

We know that the biggest problem is that a number of the original code plus his opposite number not equal to zero .

For example: 0001 + 1001 = 1010 (1 + (- 1) = - 2) 0010 + 1010 = 1100 (2 + (- 2) = --4)

So inverted design concept is directed at solving this **, since a negative is a positive number of opposite number, then we simply use a positive number Bitwise try to represent negative **.

Anti-code: Positive anti-code or equal to the original code
negative anti-code is his original code in addition to the sign bit, Bitwise.

In terms of the number of signed four binary bits, for example:

3 is a positive number, the inverted original codes are the same, may be '0011
-3 1011 is the original code, the sign bit remains unchanged, low three (011) bitwise to give (100)
so that the inverted -3 1100
gives the number of positive and negative portions of the anti-code in FIG binary notation

Here Insert Picture Description

In front of the figure, we then try to use the inverted way to solve the problem about the original code

0001+1110=1111 (1+(-1)= - 0)

The sum is equal to the opposite of each other 0, solution. Although the result is 1111 which is -0

Well, we'll try to do two negative sum

1110(-1)+1101(-2)=1011(-4)

Oh, it seems there is a new problem

(-1)+(-2)=(-4)?

But it seems a big problem, since 1011 (-4 anti-code, but the code from the original point of view, he is actually -3. Coincidence?)

We look at an example of it

1110(-1)+1100(-3)=1010(-5)

Indeed a coincidence, it seems the opposite of the problem is solved, but it puts the sum of two negative numbers wrong.

But in fact, two negative sum error in fact not a big problem. We look back and think what our purpose is? Is to solve subtraction problems, the subtraction as addition to count.

Two positive and two negative numbers together by adding, in fact, are an addition problem, but whether the sign bit Bale. The positive + negative real subtraction problems.

That is as long as the positive + negative number can not go wrong, then there is no problem. Negative plus a negative error does not matter, the nature of the negative is a positive plus sign bit only.

In the original code notation adding two negative numbers, in fact, in the case does not overflow the result is only the sign bit error only (1001 + 1010 = 0011)

Inverted negative sum error, in fact, not a big problem. We only need to add to achieve a negative two adders, two negative sign bit inverted comprising all summed bitwise, and give him the sign bit forced to a '1' on it.

So anti-code representation is already solved the problem of subtraction, he not only does not appear the case of two opposite numbers together is not zero as like the original code, but also for any of a number of positive plus negative, such as:
0001 (1) +1101 (-2) = 1110 (-1) calculated result is correct. So inverted compared to the original code, the biggest advantage lies in solving the problem of subtraction.

But we are not satisfied Why 0001 + 1110 = 1111 (1 + (- 1) = - 0) Why does -0

And although not adding two negative issues, but not a big problem, but also the problem that way. Well, Virgo. Then we introduce our big boss complement.

(Iv) complement

Complement: complement positive number equal to his original code
negative anti-code complement equal to +1.

(This is just one way complement operator, is the majority of the book to complement this sentence)

In "Principles of Computer composition", another algorithm that complement

Negative complement equal to his original code from low to high, the first ending in '1' and the right '0' remains unchanged, the left you bitwise, the sign bit unchanged.

OK, complement it finished. Goodbye! !

Somehow still there, why anti-code complement equal to plus 1, why negated from the low to high ...?

In fact, the above two paragraphs, then, we are just complement method for finding, rather than complement defined code. Many people think that we must first seek complement negate code, not really.

Those chicken thief computer scientist, not the whim of the inverted +1 is defined as the complement. Just exactly equivalent to complement the anti-code plus 1 Bale.

So, forget those negative phrase complement the book is equal to its inverse +1. This sentence will take us into the error of understanding.

Then I realized this is why I see the book, "Principles of Computer Organization", to specifically talk about the complement, anti-code repeat.

And said negative complement equal to his original code from low to high, the first ending in '1' and the right '0' remains unchanged, the left you bitwise, the sign bit unchanged.

But the above phrase, the same code is not defined complement, it is just the complement of another method for finding. It's there, tell us forget the damn phrase 'inverted +1' it is not necessary.

If you are interested in learning, complement the rigorous argument, I suggest that you can look at the "Principles of Computer Organization." It will use the 'die' concept 'congruence' is, strictly interpreted complement.

Next, I just want to talk to complement the idea.

Thought (e) complement

Thought complement, for the first time may feel quite see around, but if you are willing to stop and think about it, they will definitely feel wonderful.

Thought complement of fact from life, but we did not notice it. Clock, latitude and longitude, "Book of Changes" in the gossip.

In fact, it is thought complement similar life clock

Well, I actually do not want a similar, like this word, because the analogy is, after all, not the thing itself. And do not make me suspect that I am not a rigorous engineering monk, if I could like too rigorous, ha ha

If now the hour is now parked at 10 o'clock, so when the clock stops at eight o'clock it?

Simple, past every two hours when it is eight o'clock. Over the next ten hours time it was eight o'clock

That aside time being 10 hours, or two hours are set aside back at eight o'clock.

I.e. 10-2 = 8, and 10 + 8 = 10 (10 + 10 = 10 + 8 + 2 + 8 = 12 = 8)

12 hour full description at this time in the second lap to go, and went eight hours, so they just stopped the clock at eight o'clock.

12 so that the clock operation, called die 12 will again exceed the calculations starting from 1.

That is, 10-2 and 10 + 10 from another point of view are equivalent, it will make the hour hand pointing to eight o'clock.

Since it is equivalent to that of the clock operation, subtracting a number, in fact, equivalent to adding a further number (this number is exactly equal to the sum and subtraction 12, also referred to as the number of congruence)

This is an example of life complement the so-called modular arithmetic ideas

Here we re-emphasize the original code, anti-code and complement the introduction is to solve the problems do subtraction. In the original code, anti-code representation, we thought the subtraction into addition is a minus number, a number equal to the number of plus reverse, and found that the introduction of the sign bit, but because the sign bit less than all the dispositions caused The problem.

But from the above example, we can see that actually subtract a number, there is a limit to the value, with an operational (modular arithmetic) overflow, in fact, the equivalent of this number plus the number of congruence.

In other words, we do not introduce the concept of negative numbers, you can put the subtraction as addition to count. So let's talk about arithmetic 4-bit binary number, do not rush to introduce the sign bit. Because the idea of ​​complement, when the subtraction as addition is not necessary to introduce the sign bit.

And we can use the following example, might be able to answer another question, why the negative sign bit is '1', rather than the number of positive sign bit is '1'.

(Vi) Examples of complement

Well, then we do a subtraction four binary numbers right (not the first to introduce the sign bit)

0110 (6) -0010 (2) = 4 [6-2, but since the computer does not have a subtracter, we can not count]

This time, we think the clock operation, minus a number, is equivalent to adding another positive number (with remainder)

So what this number is it? From the clock operation, we can see that the number is exactly equal to the sum decreases in modulus.

Then the analog four binary number is how much? That is the maximum capacity of four binary number is how much? In fact, that is 2 ^ 4 = 16 = 10000B

So with the remainder 2, it means 10000-0010 = 1110 (14)

That being the case

0110(6)-0010(2)=0110(6)+1110(14)=10100(20=16+4)

OK, we have seen the results obtained in this algorithm is 10100, but for four binary numbers, the largest can hold only four (hardware decision), if we lower four bits, is exactly 0100 (4), happens to be we want results, as the most significant bit of '1', the computer will carry him into psw register bits. 8 machines will be placed in cy, x86 will be placed in cf (which we will not discuss)

This time, we try to think in four binary numbers, minus 2, plus it is equivalent to the same number more than 14 (as to why they are congruent, it is recommended to see "Principles of Computer Organization")

However subtracting 2 from another point of view, but also adding (-2). I.e., plus (-2) 14 and adding the two results actually obtained in addition to the carry bit, the result is the same.

If we put 1110 (14) is regarded as the most significant bit is the sign bit (-2) complement, which may be why a negative sign bit is '1' instead of '0'

But also has four binary bits in the symbol can only represent '-8 to 7', no action symbol length (14) and a signed number (-2) is actually the same effect.

That positive complement it? Adding a positive number, the adder may be implemented directly. So it will still complement its own.

The following figure shows four bit signed binary complement notation

Here, we find that the problem of the original code, anti-code and complement basically solved.

There is no negative zero in the complement code, as represented by 1000 -8

This is because under the above FIG complement, when the subtraction, 0001 (1) +1111 (-1) = 0000
we do not need a negative 1000 to represent 0, and put it to a predetermined -8

Negative and also solve the problem by adding the negative 1111 (-1) +1110 (-2) = 1101 (-3)

It may put a bit around, but it is no way. Actually, I think you can also complement this painting.

Very beautiful there, if you think about geography textbooks, 0 is not equivalent to the prime meridian, -8 is not 180 °, while a positive number equivalent to west, negative number is equivalent to longitude.

(Vii) why such Twos

Then we look at why why negative complement method of calculating anti-code +1

Because the absolute value of the negative inverse code plus the negative is exactly equal to 1111, plus 1, that is 1000, is the number two feed four analog

The negative complement its absolute value is the number of congruence, absolute value by subtracting the negative mold, to get his fill.

So negative complement is its inverse +1.

Around a bit of it, can only be considered clear enough that it is hard, you still do the math right. There is another method I mentioned above.

Next, I have to say about myself counted complement tips.
Here Insert Picture Description
Look above that figure.

If we -8 origin as negative. Then -5 complement is how much?

+ 3 = -8 -5
-5 -8 complement is the complement of plus 3

1000 (-8) +0011 (3) = 1011 (-5)
so that the mouth can be calculated complement -5 1011

Of course, you can also remember -1 complement is obtained subtraction operator 1111

For eight adder then, as you can complement -128 origin. When sixteen -32768 can complement origin.

Yes, 128 is a half (analog bit binary number) half 32768 65536 (mode number into two sixteen) 256

There is also very convenient, but a simple yes

Origin complement most significant bit is always '1', other bits is '0'

So simple that you can always do the addition port operator.

OK, the original code, anti-code complement trip to the end here. The first look will always complement felt around, want concise, afraid of where missing. Speaks carefully, I can not help you even feel verbose. Xie view!

Author:
link: https: //www.imooc.com/article/16813 block_id = tuijian_wz?
Source: Mu class network
article published in the original Mu-class network, please indicate the source, thank you

These are the content of the article.

In reference to this article and other online learning materials, the better to understand the original code, anti-code and complement the principles and application. It should be noted that the "principle of computer composing" this book there are more authoritative explanation, but may be more difficult to understand.

Published 24 original articles · won praise 25 · views 4103

Guess you like

Origin blog.csdn.net/qq_44972915/article/details/104436511