Original code, anti-code, complement detailed
This article explains the computer's original code, the inverse code and the complement. It also explores why to use the inverse code and the complement, and further demonstrates why the inverse code, the addition of complement to calculate the subtraction of the original code. Part of the argument if there is a wrong place, please help me to correct the cow. I hope this article will help you learn the basics of computer!
First. Number of machines and truth values
Before learning the original code, the inverse code and the complement, we need to understand the concept of machine number and truth value first.
1. Number of machines
A binary representation of a number in a computer, called the number of machines in this number. The number of machines is signed, with a number of digits at the top of the computer holding the symbol, positive 0, minus 1
For example, in the decimal number +3, the computer word length is 8 bits, converted into binary is 00000011. If it's-3, it's 10000011.
So, 00000011 and 10000011 here are the number of machines.
2. Truth value
Because the first digit is the sign bit, the form value of the machine number is not equal to the true value. For example, the above signed number 10000011, whose highest digit 1 represents a negative, whose true value is-3 instead of the form value 131 (10000011 is converted to decimal equals 131). Therefore, for the sake of distinction, the true numeric value of the number of machines with signed bits is called the truth of the number of machines.
Example: 0000 0001 of the truth value = +000 0001 = +1,1000 0001 of the Truth =–000 0001 =–1
Second. Original code, inverse code, complement the basic concept and calculation methods.
Before we explore why the machine uses the complement, let's first understand the concept of the original code, the inverse code and the complement. For a number, the computer is stored in a certain encoding. The original code, the inverse code, the complement is the machine to store a specific number of encoding methods.
1. Original code
The original code is the symbol bit plus the absolute value of the truth, that is, use the first digit to represent the symbol, the remaining bits represent values. For example, if it is 8-bit binary:
[+1] original = 0000 0001
[-1] original = 1000 0001
The first digit is the sign bit. Because the first bit is a sign bit, the range of 8-bit binary numbers is:
[1111 1111, 0111 1111]
That
[-127, 127]
The original code is the easiest way for the human brain to understand and compute.
2. Anti-code
The way to represent the inverse code is:
The inverse code of a positive number is its own
The inverse of a negative number is based on its original code, the symbol bit is unchanged, and the remaining bits are reversed.
[+1] = [00000001] original = [00000001] anti-
[-1] = [10000001] original = [11111110] anti-
It is obvious that if an inverse code represents a negative number, the human brain cannot intuitively see its value. It is usually converted to the original code and recalculated.
3. Complement
The complement method is represented by:
The complement of a positive number is its own
The complement of a negative number is based on its original code, the symbol bit unchanged, the rest of you take the counter, the last +1. (i.e. on the basis of the inverse code +1)
[+1] = [00000001] original = [00000001] anti = [00000001] Complement
[-1] = [10000001] original = [11111110] inverse = [11111111] Complement
For negative numbers, the complement notation is also not intuitive to the human brain to see its value. It is usually also necessary to convert the original code to compute its numeric value.
Third. Why use the original code, the inverse code and the complement
Before I began to study, my advice was to "memorize" the original code, the Code and the complement, and the method of calculation.
Now we know that a computer can represent a number in three ways. For positive numbers, the results are the same for three encoding methods:
[+1] = [00000001] original = [00000001] anti = [00000001] Complement
So there's no need to explain too much. But for negative numbers:
[-1] = [10000001] original = [11111110] inverse = [11111111] Complement
The original code is visible, the inverse code and the complement are completely different. Since the original code is directly recognized by the human brain and used to calculate the expression, why there will be back code and complement it?
First of all, because the human brain can know that the first digit is the sign bit, we choose the addition and subtraction of the truth region according to the sign bit in the calculation. (The concept of truth value is at the beginning of this article). But for computers, the multiplier is already the most basic operation, to be designed as simple as possible. Computer identification "symbol bit" will obviously make the computer's basic circuit design becomes very complex! So people came up with a way to take the symbol bit into operation. As we know, subtracting a positive number from the algorithm equals adding a negative number, that is: 1-1 = 1 + (-1) = 0, so the machine can only add but not subtract, so the computer operation design is simpler.
So people began to explore the method of taking the sign bit to the operation and preserving only the addition. First look at the original code:
Expression to compute decimal: 1-1=0
1-1 = 1 + (-1) = [00000001] Original + [10000001] original = [10000010] Original =-2
If you use the original code, let the sign bit also participate in the calculation, obviously for subtraction, the result is incorrect. This is why the computer does not use the original code to represent a number.
To solve the problem of subtraction in the original code, an inverse code appears:
Expression to compute decimal: 1-1=0
1-1 = 1 + (-1) = [0000 0001] Original + [1000 0001] original = [0000 0001] Anti + [1111 1110] anti = [1111 1111] anti = [1000 0000] Original =-0
It is found that the true part of the result is correct when subtraction is calculated by inverse code. And the only problem is actually in the "0" of this particular value. Although people understand that +0 and 0 are the same, 0 with symbols doesn't make any sense. And there will be [0000 0000] Original and [1000 0000] The original two encoding represents 0.
Therefore, the complement of the emergence of the resolution of the 0 symbol and two coding problems:
1-1 = 1 + (-1) = [0000 0001] Original + [1000 0001] the original = [0000 0001] Complement + [1111 1111] complement = [0000 0000] Complement =[0000 0000] Original
So 0 is represented by [0000 0000], and 0 of the previous problem does not exist. And can be represented by [1000 0000]-128:
(-1) + (-127) = [1000 0001] Original + [1111 1111] original = [1111 1111] complement + [1000 0001] complement = [1000 0000] Complement
-1-127 of the result should be-128, in the result of the complement operation, [1000 0000] complement is-128. But note that because it actually uses the previous-0 complement to represent-128, so-128 does not have the original code and the inverse code representation. (128 of the complement means [1000 0000] The complement of the original code is [0000 0000] Original, which is not true)
Using the complement, not only fixes the 0 symbol and the existence of two coding problems, but also can represent a minimum number. This is why the 8-bit binary, which uses the original code or the inverse code to denote a range of [-127, +127], uses the complement representation of [-128, 127].
Because the machine uses the complement, the 32-bit int type that is commonly used in programming can be expressed as a range of: [-231, 231-1] because the first digit represents the sign bit. While using the complement representation can save a minimum value.
Fourth-source code, back code, complement further
What is the mathematical principle behind the computer's ingenious use of symbolic bits in operations and the addition of subtraction into additions?
Imagine a clock as a 1-bit 12-digit number. If the current time is 6, I want to set the time to 4 points, what do I need to do? We can:
1. Dial back 2 hours: 6-2 = 4
2. Forward dial 10 Hours: (6 +) MoD 12 = 4
3. Forward dial 10+12=22 Hours: (6+22) mod 12 =4
The MoD in the 2,3 method refers to the modulo operation, with MoD 12 =4 16 divided by 12 after the remainder is 4.
So the clock back dial (subtraction) can be replaced by forward (addition)!
The focus now falls on how to replace a negative number with a positive number. The above example we can feel some clues and find some regularity. But mathematics is rigorous. Not by feeling.
First, we introduce a concept related to mathematics: congruence
The concept of congruence
Two integer a,b, if they are equal to the remainder of the integer m, it is said that a,b for modulus m congruence
Remember as A≡b (mod m)
Read A with B about modulo m congruence.
An example is provided:
4 MoD 12 = 4
MoD 12 = 4
MoD 12 = 4
So 4, 16, 28 about modulo 12 congruence.
Negative number modulo
The mod operation of a positive number is simple. But what about negative numbers?
The following is a mathematical definition of the MoD operation:
Above is the screenshot, "Remove boundary" symbol can not find how to enter (word pasted over after garbled). The following is the use of "L" and "J" to replace the "remove boundary" symbol for the above figure
X mod y = xy L x/y J
The formula above means:
x mod y equals x minus the lower bound of the quotient of x and y multiplied by Y.
With-3 mod 2 for example:
-3 MoD 2
= -3-2XL-3/2 J
= -3-2xl-1.5j
= -3-2x (-2)
=-3 + 4 = 1
So:
( -2) MoD 12 = 12-2=10
( -4) MoD 12 = 12-4 = 8
( -5) MoD 12 = 12-5 = 7
Start proving
And then back to the question of the clock:
Dial back 2 hours = 10 hours before dialing
Dial back 4 hours = 8 hours before dialing
Dial back 5 hours = 7 hours before dialing
Notice that the rules are found here!
Combined with the concept of congruence that was learned above. In fact:
( -2) MoD 12 = 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 to success is getting closer. To implement a positive number instead of a negative number, you only need to apply the two theorems of the same remainder:
Reflexivity:
A≡a (mod m)
This theorem is very 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, see: http://baike.baidu.com/view/79282.htm
So:
7≡7 (mod 12)
(-2) ≡10 (mod 12)
7-2≡7 + (mod 12)
Now we have a negative number that finds a positive number with the remainder. But not 7-2 = 7+10, but 7-2≡7 + (mod 12), that is, the remainder of the calculated result is equal.
Then go back to the binary question, and look at the question of 2-1=1.
2-1=2+ (-1) = [0000 0010] Original + [1000 0001] original = [0000 0010] Inverse + [1111 1110] anti-
First come to this step,-1 of the counter code indicates that it is 1111 1110. If [1111 1110] is considered to be the original code, then [1111 1110] is the original =-126, where the sign bit is removed, which is considered to be 126.
The following rules were found:
( -1) MoD 127 = 126
126 MoD 127 = 126
That
(-1) ≡126 (mod 127)
2-1≡2+126 (mod 127)
2-1 is the same as the remainder result of the 2+126! And this remainder, officially, is the result of our expected calculation: 2-1=1
So the inverse code of a number is actually the same remainder of the number for a membrane. And this membrane is not our binary, but can represent the maximum value! This is the same as a clock, after a circle, always find a correct value in the range of expression!
And 2+126 is apparently the equivalent of a clock turning round, and because the symbol bit is involved in the calculation, the right and overflow of the highest bit to form the correct result of the operation.
Since the inverse code can transform subtraction into addition, what is the complement of the computer now used? Why add 1 on the basis of an inverse code and get the right results?
2-1=2+ (-1) = [0000 0010] Original + [1000 0001] original = [0000 0010] Complement + [1111 1111] Complement
If you take [1111 1111] as the original code and remove the sign bit, then:
[0111 1111] Original = 127
In fact, on the basis of the inverse code +1, just equivalent to increase the value of the film:
( -1) MoD 128 = 127
127 MoD 128 = 127
2-1≡2+127 (mod 128)
At this point, the dial is equivalent to one round per 128 ticks. Therefore, the minimum and maximum values of the operational results expressed in the complement should be [-128, 128].
However, due to the special situation of 0, there is no way to represent 128, so the value range of the complement is [-128, 127]
I have been not good at mathematics, so if there are wrong in the text, please include a lot of advice!
Thank you for reading, I hope to help you, thank you for your support for this site!