Ask a basic question.
How does a negative number represent it in a computer?
For example, if + 8 is represented as 1000 of binary in a computer, how does-8 represent it?
It is easy to think that a binary bit (BIT) can be specified as a symbol bit. When it is equal to 0, it indicates a positive number, and when it is equal to 1, it indicates a negative number. For example, in an 8-bit server, the highest bit of each byte is the symbol bit. Then, + 8 is 00001000, while-8 is 10001000.
However, anyone looking for a "Computer Principle" will tell you that, in fact, the computer uses a 2's complement code (two's complement) to indicate a negative number.
What is a 2's complement?
It is a numerical conversion method, which must be completed in two steps:
The first step is to take the opposite value for each binary bit, and change 0 to 1, 1 to 0. For example, the opposite value of 00001000 is 11110111.
Step 2: add the value obtained in the previous step to 1. 11110111 is changed to 11111000.
Therefore, the 2's complement of 00001000 is 11111000. That is to say,-8 is represented by 11111000 in the computer (8-bit machine.
I don't know what you think. Anyway, I think it's strange. Why do we need to use such a troublesome way to represent negative numbers? Isn't it a more intuitive way?
Yesterday, I saw this problem again in a book, and then I spent some time searching for information online. Now I have understood it completely.
Benefits of 2's Complement
First, make it clear. It doesn't matter how the computer internally represents a negative number. As long as the one-to-one relationship can be maintained, negative numbers can be expressed in any way. Therefore, since you can choose any one, you should choose the most convenient method.
2's complement is the most convenient method. Its convenience is reflected in that all addition operations can be completed using the same circuit.
Or use-8 as an example.
Assume there are two representation methods. One is intuitive notation, that is, 10001000; the other is the complement notation of 2, that is, 11111000. Which of the following statements is more convenient In addition operations?
Write a computing formula, 16 + (-8) =?
The binary value of 16 is 00010000. Therefore, in intuitive notation, the addition must be written as follows:
00010000
+ 10001000
---------
10011000
As you can see, if we follow the normal addition rules, we will get the result of 10011000, and convert it to decimal to-24. Obviously, this is the wrong answer. That is to say, in this case, normal addition Rules are not applicable to addition of positive and negative numbers. Therefore, we must develop two sets of calculation rules, one for positive and negative numbers, and the other for positive and negative numbers. In the circuit, two types of circuits must be implemented for addition operations.
Now let's look at the complement representation of 2.
00010000
+ 11111000
---------
100001000
As you can see, according to the normal addition rule, the result is 100001000. Note that this is a 9-bit binary number. We have assumed that this is an 8-bit server, so the highest 9th-bit Server is an overflow location and will be automatically removed. Therefore, the result is changed to 00001000, Which is exactly 8 in decimal format, that is, the correct answer of 16 + (-8. This shows that the 2's complement expression can extend the addition operation rules to the entire Integer Set, so that the addition of All integers can be realized using a set of circuits.
The essence of 2's Complement
Before answering the 2's complement code, let's take a look at its nature, that is, how the two-step conversion methods come from.
To convert a positive number to a negative number, you only need to subtract this number from 0. For example,-8 is actually 0-8.
We know that the binary value of 8 is 00001000, and-8 can be obtained using the following formula:
00000000
-00001000
---------
Because 00000000 (subtrahend) is less than 0000100 (subtrahend), it is not enough. Recall the elementary arithmetic. What should we do if one of the subtrahend is smaller than the subtrahend? It's easy. Just ask the last one to borrow 1.
Therefore, 0000000 asked the last borrow 1, that is to say, the subtrahend are actually 100000000, and the formula is changed:
100000000
-00001000
---------
11111000
Further observation shows that 100000000 = 11111111 + 1, so the above formula can be split into two:
11111111
-00001000
---------
11110111
+ 00000001
---------
11111000
This is the two conversion steps of 2's complement code.
Why is positive addition applicable to 2's complement?
In fact, what we want to prove is that the X-Y or X + (-y) can be completed with the complement of x plus 2 of Y.
The 2's complement of Y is equal to (11111111-y) + 1. Therefore, the 2's complement of X is equal:
X + (111111-y) + 1
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, z is a negative number. In this case, we use the inverse operation of 2's complement code for Z to find the absolute positive number value corresponding to it, and then add a negative number to the front. So,
Z =-[11111111-(Z-1)] =-[11111111-(x + (111111-y) + 1-1)] = x-y
In the second case, if X is greater than Y, this means that Z must be greater than 11111111, but we have stipulated that this is an 8-bit server. The highest 9th-bit Server is an overflow bit and must be removed, this is equivalent to subtracting 100000000. So,
Z = z-100000000 = x + (11111111-y) + 1-100000000 = x-y
This proves that, under the normal addition rule, we can use the 2's complement code to obtain the correct result of adding a positive number to a negative number. In other words, as long as the computer is deployed with an addition circuit and a complementary circuit, All integers can be added.
Negative expression in Computer