Talk about original code, inverse code, complement code

Reprinted: https://www.cnblogs.com/fangle/p/6816829.html

This article starts from the original code. Deepen the understanding of the complement code by briefly describing the functions of the original code, the complement code and the complement code. And strive to allow you to complement the concept is no longer limited to: 负数的补码等于反码加一.

Students who have been exposed to computer or electronic information related courses should have seen the complement code more or less. Every time, I came up with such a paragraph on the first few pages of the textbook: 什么反码是原码除符号位,按位取反。补码等于反码加一。Then I made it inexplicably and confusedly, and then turned the page. Anyway, the content at the back didn't have much to do with the three yards.

I hadn't read it after reading it several times before. The ancients said: There are no more than three things. I saw it once when I was learning C language. Don't understand? I have read it when I read "Basic Principles of Computer Composition", but I still don't understand it! By the third year, I still didn't understand when I was on "Single-Chip Microcomputer Principles and Interface Technology". At the end of the semester, during the review, I chatted with the people in the dormitory. Talking about these codes, I said I am not very clear. Then he said how to find the code while calculating. Playing with it, I suddenly understood. I said yes, stop. Not to mention, I will sort out my thoughts during the holidays, so I have this amount. . Count discussion posts.

alright, do not piffle any more. Start our journey of original code, inverse code, and complement code.

(1) Preliminary knowledge

Recognize binary, hexadecimal. Able to convert between binary and decimal

Determined by the computer’s hardware, any data stored in the computer, its本质都是以二进制码存储。

According to the classical computer architecture framework proposed by von Neumann. A computer consists of arithmetic unit, controller, memory, input and output devices. Among them, the arithmetic unit is only an addition arithmetic unit, and there is no subtraction arithmetic unit.

So, 计算机中的没法直接做减法的,它的减法是通过加法来实现的。you might say that all subtractions in the real world can also be regarded as additions, and subtracting a number can be regarded as the opposite of adding this number. Of course yes, but the premise is to have the concept of negative numbers first. This is why a damn sign bit has to be introduced.

而且从硬件的角度上看,只有正数加负数才算减法。
正数与正数相加,负数与负数相加,其实都可以通过加法器直接相加。

The original code, the inverse code, and the complement code are generated in order to solve the problem of the computer doing subtraction and introducing sign bits (positive and negative signs).

This article may be quite long, so there is no need to read it all at once. Original code, inverse code, complement code, read by chapter. 
The key point is to talk about complements. When it comes to complements, it may be a bit confusing. It is recommended to take a pen and write out the binary numbers together.

The expression may not be clear and precise enough, please forgive me. 
(Two) the original code

Original code: It is the simplest machine number representation. Use the highest bit to represent the sign bit, '1' represents a negative sign, and '0' represents a positive sign. The other bits store the absolute binary value of the number.

If you take a four-digit binary number with a sign bit as an example

 1010  : 最高位为‘1’,表示这是一个负数,其他三位为‘010’,
      即(0*2^2)+(1*2^1)+(0*2^0)=2(‘^’表示幂运算符)
      所以1010表示十进制数(-2)。

The following figure shows the binary original code representation of some positive and negative numbers. 
image description
OK, the original code representation is very simple. Is there any, although +0 and -0 appear, it is intuitive and easy to understand. 
So, we happily started computing.

0001+0010=0011    (1+2=3)OK
0000+1000=1000    (+0+(-0)=-0) 额,问题不大
0001+1001=1010    (1+(-1)=-2)

Oh, 1+(-1)=-2this seems to be teasing me.

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

The addition of a positive number and a negative number, or the addition of a negative number and a negative number, will cause inexplicable results, which are all caused by the damn sign bit. 0 points +0and -0sums are also due to him.

So the original code, although intuitive and easy to understand, is easy to convert to positive values. But for realizing addition and subtraction, the operation rules are always too complicated. So the inverse code came.

(3) Inverse code

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

E.g:0001+1001=1010 (1+(-1)=-2) 0010+1010=1100 (2+(-2)=-4)

So the design idea of ​​the inverse code is aimed at solving this point. Since a negative number is the opposite of a positive number, then we simply use a positive number to reverse the bit to represent a negative number.

Inverse code: the inverse code of a positive number is still equal to the original code

The inverse code of a negative number is that its original code is inverted bit by bit except for the sign bit.

If take a four-digit binary number with a sign bit as an example:

3是正数,反码与原码相同,则可以表示为0011
-3的原码是1011,符号位保持不变,低三位(011)按位取反得(100)
所以-3的反码为1100

The following figure shows the binary number's complement representation of some positive and negative numbers 
image description

Facing the above picture, let's try to solve the problem of the original code with the inverse code.

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

If the numbers are opposite to each other, the sum is equal to 0 and the solution is solved. Although the result is 1111, which is -0

OK, let’s try to add two negative numbers again

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

Oh, there seems to be a new problem

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

But it seems that the problem is not big, because 1011 (is the inverse of -4, but judging from the original code, he is actually -3. Is it a coincidence?)

Let's look at another example

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

It is indeed a coincidence. It seems that the opposite number problem is solved, but it makes an error to add two negative numbers.

But in fact, adding two negative numbers is not a big problem. Let's think back to what is our purpose? It is to solve the problem of subtraction, and treat subtraction as addition.

The addition of two positive numbers and the addition of two negative numbers are actually an addition problem, but there is an unsigned bit. And positive number + negative number is the real subtraction problem.

In other words, as long as the positive number + negative number does not go wrong, then there is no problem. It's okay to add a negative number to a negative number. The essence of a negative number is to add a sign bit to a positive number.

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

The addition of the negative numbers of the one's complement is wrong, but it is not a big problem. When we only need to add to realize the addition of two negative numbers, we need to add the two negative numbers including the sign bit and take the inverted addition bit by bit, and then force the sign bit to '1'.

So the inverse code notation has actually solved the problem of subtraction. Not only will it not appear that the addition of two opposite numbers is not zero like the original code, but also for any positive number plus a negative number, such as: the 
0001(1)+1101(-2)=1110(-1) calculation result is correct of. Therefore, the biggest advantage of the inverse code compared with the original code is that it solves the problem of subtraction.

But we are still not satisfied why  0001+1110=1111 (1+(-1)=-0) why is -0it

And although it is not a big problem to add two negative numbers, it is not a big problem, it is also a problem. Well, Virgo. Next, we will introduce our big boss 补码.

(Four) complement

Complement: The complement of a positive number is equal to its original code 
, and the complement of a negative number is equal to the complement +1. 
(This is just a way to calculate the complement, most books use this sentence for the complement)

In "Principles of Computer Composition", another algorithm for complementing codes is

The complement of a negative number is equal to its original code from low to high, the first '1' of the mantissa and the '0' on the right remain unchanged, the bits on the left are reversed bit by bit, and the sign bit remains unchanged.

OK, the complement is over. Goodbye! !

It’s still inexplicable. Why is the complement equal to the complement plus 1? Why is it reversed from the low bit to the high bit..................?

In fact, the two paragraphs above are just the method of complementing the code, not the definition of the complementing code. Many people think that to ask for the complement is to ask for the complement first, but it is not.

The computer scientists of those chicken thieves do not define the complement +1 as the complement on a whim.只不过是补码正好就等于反码加1罢了。

So, forget that the complement of a negative number in those books is equal to its inverse +1. This sentence brings us into a misunderstanding.

This is why I later understood why the book "Principles of Computer Organization" that I read had to talk about the complement first, and then the inverse code.

Then it is said that the complement of a negative number is equal to its original code from low to high, the first '1' of the mantissa and the '0' on the right remain unchanged, the bits on the left are reversed bit by bit, and the sign bit remains unchanged.

But the above sentence is also not the definition of complement, it is just another way of finding complement.它的存在,告诉我们忘记那句该死的‘反码+1’它并不是必须的。

If you are interested in understanding the strict terms of complements, I suggest you to read "Principles of Computer Composition". It uses the concepts of'modulus' and'congruence' to rigorously explain the complement.

Next, I just want to talk about the idea of ​​complements.

(5) The idea of ​​complement

The idea of ​​complementing code may feel very convoluted the first time you see it, but if you are willing to stop and think about it, it will definitely feel very wonderful.

The idea of ​​complementing code actually comes from life, but we haven't noticed it. Clock, latitude and longitude, gossip in the Book of Changes.

The idea of ​​complementing code is actually similar to the clock in life

Well, I actually don't want to use similar words like this, because the analogy is not the thing itself after all. And the lack of rigor makes me suspect that I am not an engineering monk, as if I have been rigorous, haha

If the hour hand now stops at 10 o'clock, when will the hour hand stop at eight o'clock?

Simply, it was eight o'clock in the past two hours. It will be eight o'clock in the next ten hours

In other words, the time is set to 10 hours, or 2 hours is set to 8 o'clock.

That is, 10-2=8, and 10+10=8 (10+10=10+2+8=12+8=8)

At this time, it means that the hour hand is moving for the second lap, and it has gone for another 8 hours, so the hour hand stops at eight o'clock again.

Therefore, 12 is called a modulus in clock calculations. If 12 is exceeded, it will be counted from 1 again.

In other words, 10-2 and 10+10 are equivalent from another point of view, they both make the hour hand point to eight o'clock.

Since it is equivalent, in clock operation, subtracting a number is actually equivalent to adding another number (the addition of this number and the subtraction is exactly equal to 12, which is also called a congruence number)

This is the life example of the so-called modular arithmetic thought of complements

Here, we are again 强调原码,反码,补码的引入是为了解决做减法的问题。in the original code, one's complement notation, our thinking of turning subtraction into addition is to subtract a number, which is equal to adding the opposite number of a number. As a result, we found that the sign bit was introduced, but it was caused by the sign bit. All kinds of unexpected questions.

But from the above example, we can see that a number is actually subtracted,对于数值有限制,有溢出的运算(模运算)来说,其实也相当于加上这个数的同余数。

In other words, we 不引入负数的概念,就可以把减法当成加法来算. So next we talk about the operation of 4-bit binary numbers, and there is no need to rush to introduce the sign bit. Because 补码的思想,把减法当成加法时并不是必须要引入符号位的of

And we can answer another question through the following example,为什么负数的符号位是‘1’,而不是正数的符号位是‘1’。

(6) Complementary code example

Okay, let's do a four-digit subtraction of binary numbers (without introducing the sign bit)

0110(6)-0010(2) [6-2=4, but since there is no subtractor in the computer, we can’t calculate it]

At this time, we think about the clock operation, subtracting a number can be equivalent to adding another positive number (congruent number)

So what is this number? From the clock calculation, we can see that the addition of this number and the subtraction is exactly equal to the modulus.

So what is the modulus of a four-digit binary number? In other words, what is the maximum capacity of a four-digit binary number?其实就是2^4=16=10000B

Then the congruence of 2 is equal to 10000-0010=1110 (14)

In that case

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

OK, we see that the result obtained according to this algorithm is 10100, but for a four-digit binary number, the maximum can only store 4 digits (determined by the hardware). If we lower four digits, it happens 0100(4)to be the result we want. As for the highest position ‘1’, the computer will put it in the psw寄存器进位位中。8-bit machine and it will be in the cymiddle, and the x86 will be in the cfmiddle (we will not discuss this)

At this time, let's think about it again in a four-digit binary number, subtracting 2 is equivalent to adding its congruence 14 (as for why they are congruent, it is still recommended to read "Computer Composition Principles")

But subtracting 2, from another perspective, is also adding (-2). That is , the binary result obtained by adding (-2)and adding 14is the same except for the carry bit.

If we regard 1110(14)the highest bit as (-2)the complement of the sign bit , this may be why the sign bit of a negative number is ‘1’not ‘0’,

Moreover, in a four-bit binary number with a signed bit, the only thing that can be represented is the function of an ‘-8~7’unsigned bit and (14)the effect of a signed number (-2)are actually the same.

What about the complement of a positive number? Add a positive number and the adder can be implemented directly. So its complement is still itself.

The following figure shows the two's complement representation of a signed four-bit binary 
image description

At this point, we found that the problem of the original code and the inverse code, the complement code has basically been solved.

There is no negative zero in the complement,因为1000表示-8

This is because according to the above complement diagram, when doing subtraction, 0001(1)+1111(-1)=0000 
we no longer need one 1000to represent 负0it, so we define it as-8

The problem of adding negative numbers to negative numbers is also solved1111(-1)+1110(-2)=1101(-3)

It may be a bit convoluted, but there is really no way. In fact, I think the complement code can still be drawn like this. 
image description

Is it beautiful? If you think about geography textbooks, 0 is not equivalent to the original meridian, -8 is not 180°, and positive numbers are equivalent to west longitudes, and negative numbers are equivalent to east longitudes.

(7) Why do you ask for the complement in this way

Then let's take a look at why the complement of a negative number is the one's complement +1

Because the inverse code of a negative number plus the absolute value of this negative number is exactly equal to 1111, plus 1, it is 1000, which is the modulus of a four-digit binary number

The complement of a negative number is the congruence of its absolute value, which can be obtained by subtracting the absolute value of the negative number from the modulus.

So the complement of a negative number is its complement +1.

It's a bit convoluted, I can only say that it's hard to figure it out clearly, so you should figure it out by yourself. There is another algorithm I mentioned above.

Next, I want to talk about my own tips for calculating complements.

Look at the picture above.

If we regard -8 as the origin of negative numbers. So what is the complement of -5?

-5=-8+3

-5's complement is -8's complement plus 3

1000(-8) +0011(3)=1011(-5)

So it can be calculated by mouth-5的补码是1011

Of course, you can also remember to -1的补码是1111subtract from oral arithmetic to get

For an eight-bit adder, it can be regarded -128as the origin of the complement. Sixteen bits can be used -32768as the origin of the complement.

Yes, it 128is 256half 32768of 65536( the modulus of an eight-bit binary number) and half of the ( modulus of a sixteen-bit binary number)

It’s also very convenient to have it, and it’s simple

The origin of the complement code is always the highest bit ‘1’, the other bits are‘0’

So doing addition is always easy to calculate.

OK, the original code, the inverse code, and the complement code journey ends here. The complement code always feels very convoluted the first time I look at it. If I want to be brief and concise, I am afraid of missing something. Speaking in detail, it is inevitable that even I feel verbose. Thank you!

Guess you like

Origin blog.csdn.net/modi000/article/details/113529336