Detailed explanation of original code, complement and complement, detailed explanation of original code and complement
This article explains the computer's source code, complement and complement. It also explores in depth why the complement and complement are used, and further demonstrates why the complement of the complement and complement can be used to calculate the source code. Subtraction. If there is anything wrong in the demonstration part, please help everyone to correct it! I hope this article will help everyone to learn the basics of computer!
1. Number of machines and truth values
Before learning source code, complement code and complement code, you need to understand the concepts of machine numbers and truth values.
1.Number of machines
A binary representation of a number in a computer, called the machine number of this number. Machine numbers are signed. The most significant bit of a number is used to store the sign in the computer. Positive numbers are 0 and negative numbers are 1.
For example, if the number in decimal is +3, the word length of the computer is 8 bits, and when converted to binary, it is 00000011. If it is -3, it is 10000011.
Then, 00000011 and 10000011 are the number of machines.
Truth
Because the first bit is the sign bit, the formal value of the machine number is not equal to the real value. For example, the above signed number 10000011, whose highest digit 1 represents negative, its real value is -3 instead of the formal value 131 (10000011 converted to decimal equals 131). Therefore, for the sake of distinction, the true value corresponding to the number of machines with a signed bit is called the true value of the machine number.
Example: 0000 0001 truth value = +000 0001 = +1, 1000 0001 truth value = –000 0001 = –1
2. The basic concepts and calculation methods of source code, complement code and complement code.
Before exploring why machines use two's complement, let's first understand the concepts of source code, complement and complement. For a number, the computer uses a certain encoding method to store. Source code, complement, complement is machine storage. A specific number encoding method.
Original code
The original code is the absolute value of the sign bit plus the true value, that is, the first bit represents the sign, and the remaining bits represent the value. For example, if it is 8-bit binary:
[+1] Original = 0000 0001
[-1] Original = 1000 0001
The first bit is the sign bit. Because the first bit is the sign bit, the range of an 8-bit binary number is:
[1111 1111, 0111 1111]
which is
[-127, 127]
The original code is the easiest way for the human brain to understand and calculate.
Reverse code
The inverse representation is:
The inverse of a positive number is itself
The negative code of the negative number is based on its original code, the sign bit is unchanged, and the remaining bits are inverted.
[+1] = [00000001] Original = [00000001]
[-1] = [10000001] original = [11111110]
It can be seen that if a negative code represents a negative number, the human brain cannot intuitively see its value. Usually it is converted to the original code and then calculated.
Complement
The two's complement representation is:
The complement of a positive number is itself
The complement of a negative number is based on its original code, the sign bit is unchanged, the remaining bits are inverted, and finally +1. (That is, +1 on the basis of the complement)
[+1] = [00000001] original = [00000001] reverse = [00000001] complement
[-1] = [10000001] original = [11111110] reverse = [11111111] complement
For negative numbers, the two's complement representation cannot be visualized by the human brain. Usually it needs to be converted into the original code to calculate its value.
Three. Why use source code, complement code and complement code
Before I start deep learning, my learning suggestion is to "remember" the original code, inverse and complement, and how to calculate it.
Now we know that a computer can represent a number in three ways of encoding. For positive numbers, the results of all three ways of encoding are the same:
[+1] = [00000001] original = [00000001] reverse = [00000001] complement
So no need to explain too much. But for negative numbers:
[-1] = [10000001] original = [11111110] reverse = [11111111] complement
It can be seen that the original code, the complement code and the complement code are completely different. Since the original code is directly recognized by the human brain and used to calculate the representation, why are there any complement codes and complement codes?
First of all, because the human brain can know that the first bit is the sign bit, when calculating, we will select the addition and subtraction of the truth value area based on the sign bit. (The concept of truth value is at the beginning of this article). But for computers, addition and subtraction Multiplier is already the most basic operation. It should be designed as simple as possible. The computer's identification of the "sign bit" will obviously make the computer's basic circuit design very complicated! So people have come up with a method to involve the sign bit in the operation. We know According to the algorithm, subtracting a positive number is equal to adding a negative number, that is: 1-1 = 1 + (-1) = 0, so the machine can only add and not subtract, so the design of computer operations is simpler.
So people began to explore the method of involving the sign bit, and only retaining the addition. First look at the original code:
Calculate decimal expression: 1-1 = 0
1-1 = 1 + (-1) = [00000001] original + [10000001] original = [10000010] original = -2
If it is expressed in the original code, and the sign bit is also involved in the calculation, it is obvious that the result is incorrect for subtraction. This is why the computer does not use the original code to represent a number.
In order to solve the problem of subtraction in the original code, the reverse code appeared:
Calculate decimal expression: 1-1 = 0
1-1 = 1 + (-1) = [0000 0001] original + [1000 0001] original = [0000 0001] inverse + [1111 1110] inverse = [1111 1111] inverse = [1000 0000] original = -0
It was found that the subtraction calculation of the inverse code is correct, and the true value of the result is correct. The only problem actually appears in the special value "0". Although people understand that +0 and -0 are the same, 0 is signed It doesn't make any sense. And there will be [0000 0000] original and [1000 0000] original two codes representing 0.
The emergence of two's complement solves the sign of 0 and two encoding problems:
1-1 = 1 + (-1) = [0000 0001] original + [1000 0001] original = [0000 0001] supplement + [1111 1111] supplement = [0000 0000] supplement = [0000 0000] original
In this way, 0 is represented by [0000 0000], and the previous problematic -0 does not exist. And it can be represented by [1000 0000] -128:
(-1) + (-127) = [1000 0001] original + [1111 1111] original = [1111 1111] supplement + [1000 0001] supplement = [1000 0000] supplement
The result of -1-127 should be -128. In the result of using the two's complement operation, [1000 0000] is -128. But note that because -128 is actually used to represent -128, so- 128 does not have the original code and the inverse code. (The complement code for -128 represents [1000 0000]. The original code is [0000 0000], which is incorrect.)
The use of two's complement not only fixes the sign of 0 and the problem of two encodings, but also can represent an additional lowest number. This is why 8-bit binary, using the original or reverse code for the range [-127, + 127], and the range represented by two's complement is [-128, 127].
Because the machine uses two's complement, the range of 32-bit int types commonly used in programming can be expressed as: [-231, 231-1] Because the first bit represents the sign bit. When using two's complement, it can be more Save a minimum.
Four original code, complement code, complement code
The computer cleverly participates in the sign bit operation, and changes the subtraction into the addition. What mathematical principle is behind it?
Think of a clock as a 1-digit decimal number. If the current time is 6 o'clock, I want to set the time to 4 o'clock. What can I do? We can:
1. Call back 2 hours: 6-2 = 4
2. Dial 10 hours forward: (6 + 10) mod 12 = 4
3. Dial 10 + 12 = 22 hours forward: (6 + 22) mod 12 = 4
The mod in the methods 2 and 3 refers to the modulo operation.16 mod 12 = 4 that the remainder after dividing 16 by 12 is 4.
So the clock dial back (subtraction) can be replaced by dial forward (addition)!
The focus now is on how to use a positive number instead of a negative number. In the above example we can feel some clues and find some rules. But mathematics is rigorous. We cannot rely on feelings.
First introduce a related concept in mathematics: congruence
Concept of congruence
Two integers a, b, if they are equal to the remainder obtained by dividing by the integer m, then a, b is congruent for the modulus
Let it be a ≡ b (mod m)
Read as a and b congruences on module m.
for example:
4 mod 12 = 4
16 mod 12 = 4
28 mod 12 = 4
So 4, 16, 28 are congruences on modulo 12.
Negative modulo
Mod operations on positive numbers are simple. But what about negative numbers?
Here is the mathematical definition of mod operation:
The above is the "Remove Lower Bound" symbol. I cannot find how to enter it.
x mod y = x-y L x / y J
The above formula means:
x mod y is equal to x minus y times the lower bound of the quotient of x and y.
Take -3 mod 2 as an example:
-3 mod 2
= -3-2xL -3/2 J
= -3-2xL-1.5J
= -3-2x (-2)
= -3 + 4 = 1
and so:
(-2) mod 12 = 12-2 = 10
(-4) mod 12 = 12-4 = 8
(-5) mod 12 = 12-5 = 7
Start to prove
Back to the question of clocks:
2 hours call back = 10 hours before call
4 hours call back = 8 hours before call
5 hours call back = 7 hours before call
Attention, the rules found here!
Combine the concepts of congruence learned above. Actually:
(-2) mod 12 = 10
10 mod 12 = 10
-2 and 10 are congruent.
(-4) mod 12 = 8
8 mod 12 = 8
-4 and 8 are congruent.
The distance is getting closer and closer. To replace negative numbers with positive numbers, only two theorems of congruence numbers need to be applied:
Reflexivity:
a ≡ a (mod m)
This theorem is pretty obvious.
Linear Operation Theorem:
If a ≡ b (mod m), c ≡ d (mod m) then:
(1) a ± c ≡ b ± d (mod m)
(2) a * c ≡ b * d (mod m)
If you want to see the proof of this theorem, please see: http://baike.baidu.com/view/79282.htm
and so:
7 ≡ 7 (mod 12)
(-2) ≡ 10 (mod 12)
7 -2 ≡ 7 + 10 (mod 12)
Now we have a negative number and found its positive congruence. But instead of 7-2 = 7 + 10, but 7 -2 ≡ 7 + 10 (mod 12), that is, the remainder of the calculation result is equal.
Let's go back to the binary problem and have a look: the problem of 2-1 = 1.
2-1 = 2 + (-1) = [0000 0010] original + [1000 0001] original = [0000 0010] reverse + [1111 1110] reverse
First come to this step, the inverse code of -1 is 1111 1110. If [1111 1110] is considered as the original code here, then [1111 1110] original = -126, and the sign bit is removed here, which is considered to be 126.
Found the following rules:
(-1) mod 127 = 126
126 mod 127 = 126
which is:
(-1) ≡ 126 (mod 127)
2-1 ≡ 2 + 126 (mod 127)
The remainder result of 2-1 and 2 + 126 is the same! And this remainder, officially our expected calculation result: 2-1 = 1
So the inverse of a number is actually the congruence of this number with a film. And this film is not our binary, but the maximum value that can be represented! This is the same as a clock. Can find a correct value in the representable range!
And 2 + 126 is obviously equivalent to a round of clocks, and because the sign bit is involved in the calculation, it happens to form the correct operation result with the most significant bit of the overflow.
Since the complement can change the subtraction into an addition, what about the two's complement used by computers now? Why can I get the correct result by adding 1 to the complement?
2-1 = 2 + (-1) = [0000 0010] original + [1000 0001] original = [0000 0010] complement + [1111 1111] complement
If [1111 1111] is regarded as the original code and the sign bit is removed, then:
[0111 1111] Original = 127
In fact, +1 on the basis of the inverse code is only equivalent to increasing the value of the film:
(-1) mod 128 = 127
127 mod 128 = 127
2-1 ≡ 2 + 127 (mod 128)
At this time, the dial is equivalent to rotating once every 128 scales. So the minimum and maximum values of the operation results expressed in two's complement should be [-128, 128].
However, due to the special case of 0, there is no way to represent 128, so the range of the two's complement value is [-128, 127]
I have not been good at mathematics, so if there is something wrong in the text, please include it and give pointers!
Author: Zhang Zaiqiu
Source: http://www.cnblogs.com/zhangziqiu/
The copyright of this article belongs to the author and the blog garden. Reprinting is welcome, but this paragraph statement must be retained without the author's consent, and a link to the original text should be given in the obvious position of the article page, otherwise the right to pursue legal responsibility is reserved.