Deep understanding of source code, reverse code, complement code, and reverse Code complement
This article describes the computer's original code, reverse code, and complement code. in addition, I thoroughly explored why the back code and complement code should be used, and further demonstrated why the back code and the addition of the complement code can be used to calculate the subtraction of the original code. if there is something wrong with the argument, please help me correct it! I hope this article will help you learn computer basics!
I. machine count and real value
Before learning the original code, reverse code, and complement code, you must first understand the concept of the number of machines and the real value.
1. Number of machines
The binary representation of a number in a computer, called the number of machines. The number of machines is signed. in a computer, the highest bit of a number is used to store the symbols. The positive number is 0, and the negative number is 1.
For example, if the number is + 3 in decimal format and the computer word length is 8 bits, the conversion to binary format is 00000011. -3 means 10000011.
In this case, 00000011 and 10000011 are the number of machines.
2. True Value
Because the first digit is the symbol bit, the form value of the machine number is not equal to the real value. For example, if the above signed number is 10000011, its highest bit 1 indicates negative, and its real value is-3 instead of the form value 131 (10000011 is converted to decimal equal to 131 ). Therefore, for the sake of distinction, the real value corresponding to the number of machines with signs is called the true value of the number of machines.
Example: the true value of 0000 0001 = + 000 0001 = + 0001 0001 =-000 =-1
Ii. Basic Concepts and calculation methods of the original code, back code, and complement code.
Before finding out why the machine needs to use the complement code, let's first understand the concept of the original code, the reverse code, and the complement code. for a number, the computer must use a certain encoding method for storage. the source code, the reverse code, and the supplementary code are used to store a specific number.
1. Original code
The original code is the absolute value of the symbol bit plus the true value, that is, the first digit represents the symbol, and the other digits represent the value. For example, if it is an 8-bit binary:
[+ 1] original = 0000 0001
[-1] original = 1000 0001
The first digit is the symbol bit. Because the first digit is the symbol bit, the value range of the 8-bit binary number is:
[1111 1111 , 0111 1111]
That is
[-127 , 127]
The original code is the easiest way for the human brain to understand and calculate.
2. Anti-code
The expression of the anti-code is as follows:
The inverse code of positive numbers is itself
The negative number is based on the original code, the symbol bit remains unchanged, and the other bits are reversed.
[+ 1] = [00000001] original = [00000001]-
[-1] = [10000001] original = [11111110] Reverse
It can be seen that if an anticode represents a negative number, the human brain cannot intuitively see its value. It is usually converted to the original code and then computed.
3. Complement
The expression of the complement code is as follows:
Positive complement is itself
The complement of a negative number is based on the original code, the symbol bit remains unchanged, and the rest of you get the reverse code. Finally, + 1 (that is, + 1 on the basis of the reverse code)
[+ 1] = [00000001] original = [00000001] inverse = [00000001] Complement
[-1] = [10000001] original = [11111110] = [11111111]
For negative numbers, the human brain cannot directly see the value of the complement expression. It is usually necessary to convert the value to the original code to calculate its value.
Iii. Why should I use the original code, reverse code, and complement code?
Before I start to study in depth, I suggest "memorizing" the original code, the expression of the reverse code and the complement code, and the calculation method.
Now we know that the computer can have three encoding methods to represent a number. For positive numbers, the results of the three encoding methods are the same:
[+ 1] = [00000001] original = [00000001] inverse = [00000001] Complement
So there is no need to explain too much, but for negative numbers:
[-1] = [10000001] original = [11111110] = [11111111]
It can be seen that the original code, the reverse code and the complement code are completely different. Since the original code is directly recognized by the human brain and used for computation and representation, why is there a reverse code and a complement?
First, because the human brain can know that the first digit is the symbol bit, During computation, We will select the addition and subtraction of the true value area based on the symbol bit. (the concept of true value starts at the beginning of this article ). however, for computers, the addition and subtraction of multiplier is already the most basic operation, and the design should be as simple as possible. computer identification of "symbol bit" will obviously make the computer's basic circuit design very complex! So people came up with a method to include the symbol bit in the operation. we know that, according to the algorithm minus a positive number equals to adding a negative number, that is, 1-1 = 1 + (-1) = 0, so the machine can only add and not subtract, in this way, the design of computer operations is simpler.
As a result, people began to explore the method of adding the symbol bit and only keep the addition. First, let's look at the original code:
Decimal expression: 1-1 = 0
1-1 = 1 + (-1) = [00000001] original + [10000001] original = [10000010] original =-2
If the original code is used for calculation, the result is incorrect for subtraction. That is why the computer does not use the original code to represent a number.
In order to solve the problem of original code subtraction, there is a reverse code:
Decimal expression: 1-1 = 0
1-1 = 1 + (-1) = [0000 0001] + [1111 1110] = [1111 1111] = [1000 0000] =-0
It is found that the negative code is used to calculate and subtract, and the true value of the result is correct. the only problem actually occurs in the special value "0. although we understand that + 0 is the same as-0, 0 is meaningless. in addition, there will be two encoding formats: [0000 0000] original and [1000 0000] original, indicating 0.
The occurrence of the complement solves the problem of the zero symbol and two encodings:
1-1 = 1 + (-1) = [0000 0001] original + [1000 0001] original = [0000 0001] supplement + [1111 1111] Supplement = [0000 0000 0000] Supplement = [0000] original
In this way, 0 is represented by [0000 0000], and-0, which previously encountered a problem, does not exist. In addition, [1000 0000] can be used to represent-128:
(-1) + (-127) = [1000 0001 1111] original + [1111 1111 1111] original = [1000 0001] supplement + [1000 0000] Supplement = []
-The result of 1-127 should be-128. in the result of the complement operation, [1000 0000] is-128. however, because the previous-0 complement code is used to represent-128,-128 does not represent the original code or reverse code. ("-128" indicates that the source code [1000 0000] is [0000 0000], which is incorrect)
Using the complement Code not only fixes the zero sign and two encodings, but also represents a minimum number. this is why the range of the 8-bit binary code expressed by the original code or the back code is [-127, + 127], and the range indicated by the complement code is [-128,127].
Because the machine uses the complement code, the 32-bit int type commonly used in programming can be expressed in the range of [-231,231-1] Because the first digit represents the symbol bit. you can save one more minimum value when using the complement representation.
Four original codes, anti-code, re-coding
What mathematical principles does a computer use to skillfully calculate symbols and convert subtraction into addition?
Think of a clock as a one-digit 12-digit number. If the current time is, What should I do if I want to set the time? We can:
1. 2 hours for callback: 6-2 = 4
2. Forward 10 hours: (6 + 10) mod 12 = 4
3. Forward dial 10 + 12 = 22 hours: (6 + 22) mod 12 = 4
Mod in method 2 and 3 refers to the modulo operation. 16 mod 12 = 4 means the remainder after dividing 16 by 12 is 4.
Therefore, you can use forward dial (addition) to replace the result of clock push (subtraction!
Now the focus is on how to replace a negative number with a positive number. The above examples show some clues and discover some patterns. But mathematics is rigorous.
First, we will introduce a concept related to mathematics: Same
Same concept
Two integers a and B. If they are equal to the remainder obtained by dividing them by an integer m, they are called a and B.
As a comment B (mod m)
Read as a and B.
Example:
4 mod 12 = 4
16 mod 12 = 4
28 mod 12 = 4
So 4, 16, and 28 are the same as the modulo 12.
Negative modulo
It is very easy to perform mod operations on positive numbers. But what about negative numbers?
The mathematical definitions of mod operations are as follows:
As shown in the preceding figure, the "Lower Bound" symbol cannot be found (garbled characters are pasted in word). below is the "Lower Bound" symbol replaced by "L" and "J:
x mod y = x - y L x / y J
The formula above indicates:
X mod y equals x minus y multiplied by 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
Therefore:
(-2) mod 12 = 12-2=10
(-4) mod 12 = 12-4 = 8
(-5) mod 12 = 12 - 5 = 7
Start proof
Back to the clock issue:
2 hours for callback = 10 hours for the previous dial
Callback 4 hours = previous dial 8 hours
Call back for 5 hours = dial before 7 hours
Note: The Rule found here!
In combination with the concept of remainder learned above. In fact:
(-2) mod 12 = 10
10 mod 12 = 10
-2 and 10 are the same.
(-4) mod 12 = 8
8 mod 12 = 8
-4 and 8 are the same.
The distance from success is getting closer and closer. To replace negative numbers with positive numbers, you only need to use two theorems of the same remainder:
Anti-body:
a ≡ a (mod m)
This theorem is very obvious.
Linear operation theorem:
If a branch B (mod m), c Branch d (mod m) then:
(1) a ± c ≡ B ± d (mod m)
(2) a * c branch B * d (mod m)
If you want to see the proof of this theorem, see: http://baike.baidu.com/view/79282.htm
Therefore:
7 ≡ 7 (mod 12)
(-2) ≡ 10 (mod 12)
7 -2 ≡ 7 + 10 (mod 12)
Now we find a negative number with the same positive number as the remainder. but it is not 7-2 = 7 + 10, but 7-2 then 7 + 10 (mod 12), that is, the remainder of the calculation result is equal.
Next, let's go back to the binary problem. Let's take a look at the problem: 2-1 = 1.
2-1 = 2 + (-1) = [0000 0010 1000] original + [0001 0000] original = [0010 1111] Reverse + [1110] Reverse
First to this step, the anti-code of-1 indicates 1111 1110. if [1111 1110] is considered as the source code, [1111 1110] is considered as-126. Here, the symbol bit is removed, that is, 126.
The following rule is found:
(-1) mod 127 = 126
126 mod 127 = 126
That is:
(-1) ≡ 126 (mod 127)
2-1 ≡ 2+126 (mod 127)
The result of the remainder of 2-1 and 2 + 126 is the same! The remainder officially calculates the expected result: 2-1 = 1
So the anticode of a number is actually the number of the same remainder of a film. The film is not our binary, but the maximum value we can represent! This is the same as a clock. After turning around, you can always find a correct value in the displayed range!
The 2 + 126 is clearly equivalent to switching around the clock, and because the symbol bit is involved in the calculation, it is exactly the same as the overflow highest bit to form the correct calculation result.
Since anticode can convert subtraction into addition, what is the complement code used by the computer now? Why can we still get the correct result by adding 1 on the basis of the anti-code?
2-1 = 2 + (-1) = [0000 0010 1000] original + [0001 0000] original = [0010 1111] supplement + [1111] supplement
If [1111 1111] is used as the source code and the symbol bit is removed, then:
[0111 1111] original = 127
In fact, on the basis of the reverse code + 1, it is equivalent to adding the value of the membrane:
(-1) mod 128 = 127
127 mod 128 = 127
2-1 ≡ 2+127 (mod 128)
At this time, the dial is equivalent to converting every 128 scales. Therefore, the minimum and maximum values of the computation result indicated by the complement code should be [-128,128].
However, due to the special case of 0, there is no way to represent 128. Therefore, the value range of the complement code is [-128,127].
Source: http://www.cnblogs.com/zhangziqiu/