About 2's complement

Ask a basic question.
How are negative numbers represented in computers?
For example, +8 is represented in the computer as 1000 in binary, so how is -8 represented?
It is easy to think that a binary bit (bit) can be specially specified as the sign bit. When it is equal to 0, it represents a positive number, and when it is equal to 1, it represents a negative number. For example, in an 8-bit machine, the highest bit of each byte is specified as the sign bit. Then, +8 is 00001000, and -8 is 10001000.
However, if you find any "Computer Principles", it will tell you that, in fact, the computer uses two's complement (Two's Complement) to represent negative numbers.
What is 2's complement?
It is a numerical conversion method, which is completed in two steps:
in the first step, each binary bit takes the opposite value, 0 becomes 1, and 1 becomes 0. For example, the opposite of 00001000 is 11110111.
In the second step, add 1 to the value obtained in the previous step. 11110111 becomes 11111000.
So, the 2's complement of 00001000 is 11111000. That is to say, -8 is represented by 11111000 in a computer (8-bit machine).
I don't know what you think. Anyway, I think it's very strange. Why do you use such a troublesome way to represent negative numbers? Isn't it better to use a more intuitive way?
Yesterday, I saw this problem again in a book, and then I spent a little time looking for information on the Internet, and now I finally understand it completely.
The benefits of 2's complement
First , let's be clear. It doesn't really matter what way the computer uses to represent negative numbers. Negative numbers can be represented in any way as long as the one-to-one correspondence can be maintained. Therefore, since you can choose arbitrarily, you should choose the most convenient way.
2's complement is the most convenient way. Its convenience is that all addition operations can be done using the same circuit.
Again, take -8 as an example.
Suppose there are two representations. One is the intuition notation, which is 10001000; the other is the 2's complement notation, which is 11111000. Which notation is more convenient for addition operations?
Write a random calculation formula,
the binary representation of 16 + (-8) = ? 16 is 00010000, so using the intuitive notation, the addition should be written as:
 00010000
+ 10001000
---
 10011000
can be seen, if according to The normal addition rule will get the result of 10011000, which is -24 when converted to decimal. Obviously, this is the wrong answer. That is to say, in this case, the normal addition rules do not apply to the addition of positive and negative numbers, so two sets of arithmetic rules must be formulated, one for positive numbers plus positive numbers, and one for positive numbers plus negative numbers. From the circuit, it is necessary to make two circuits for the addition operation.
Now, let's look at 2's complement notation.
 00010000
+11111000
--- --- ---
100001000 As
you can see, according to the normal addition rules, the result is 100001000. Note that this is a 9-bit binary number. We have assumed that this is an 8-bit machine, so the highest 9th bit is an overflow bit and is automatically rounded off. So, the result becomes 00001000, which in decimal is exactly 8, which is the correct answer for 16 + (-8). This shows that the 2's complement representation can extend the addition operation rules to the entire set of integers, so that the addition of all integers can be realized with a set of circuits.
The essence of 2's complement
Before answering why 2's complement can correctly implement the addition operation, let's take a look at its essence, that is, how the two-step conversion method comes from.
To convert a positive number into a corresponding negative number, in fact, just subtract this number from 0. For example, -8 is actually 0-8.
Knowing that the binary value of 8 is 00001000, -8 can be obtained by the following formula:
 00000000
-00001000
---
Because 00000000 (minuend) is less than 0000100 (minutra), it is not enough to subtract. Recall elementary school arithmetic. What do we do if one of the minuends is less than the subtrahend? It's very simple, just ask the previous person to borrow 1.
Therefore, 0000000 also asks the previous digit to borrow 1, that is to say, the minuend is actually 100000000, and the formula is rewritten as:
100000000
-00001000
---
 11111000
Further observation, you can find that 100000000 = 11111111 + 1, so the above formula can be split into two:
 11111111
-00001000
--------
 11110111
+00000001
---------
 11111000
The two's complement conversion steps of 2's complement are like this of.
Why does addition of positive numbers work for 2's complement?
In effect, what we're trying to prove is that XY or X+(-Y) can be done with X plus Y's 2's complement.
The 2's complement of Y is equal to (11111111-Y)+1. So, X plus Y's 2's complement equals:
X + (11111111-Y) + 1
We assume that the result of this formula is equal to Z, that is, Z = X + (11111111-Y) + 1
Next, we will discuss it in two cases.
In the first case, if X is less than Y, then Z is a negative number. At this time, we use the inverse operation of 2's complement for Z to find the absolute value of its corresponding positive number, and then add a negative sign in front of it. So,
Z = -[11111111-(Z-1)] = -[11111111-(X + (11111111-Y) + 1-1)] = X - Y
The second case, if X is greater than Y, it means Z is definitely greater than 11111111, but we stipulate that this is an 8-bit machine, and the highest 9th bit is the overflow bit, which must be rounded off, which is equivalent to subtracting 100000000. So,
Z = Z - 100000000 = X + (11111111-Y) + 1 - 100000000 = X - Y
This proves that under normal addition rules, you can use 2's complement to get the correct addition of positive and negative numbers result. In other words, the computer can complete the addition of all integers as long as the addition circuit and the complement circuit are deployed.
(over)

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326765613&siteId=291194637