Source code, reverse code, and supplemental code.
Original code representation (the highest bit is used as the symbol bit, 0 indicates a positive number, and 1 indicates a negative number):
Reverse code representation (a negative integer is a positive integer that is reversed by bit):
Complement representation (the negative integer is a positive integer that is equal to 1 after bitwise subtraction):
Assume that only four binary digits are used to represent an integer. The binary value ranges from 0000 to 1111, and can represent 2 ^ 4, that is, 16 integers.Put them on a dial with 16 scales:
Figure 1
Figure 1As shown in,Because there are only four binary digits, there are only 16 bit modes on the dial.Each scale of 16 scales corresponds to a bit mode. Obviously, each bit mode can be used to represent an integer.If they are all used to represent positive integers, they can only represent 0 to 15.
How does one represent a negative number?
To be fair, the scale of a positive number and a negative number is divided into half.Cut the dial into two halves, with half representing a positive number and half representing a negative number. if we specify the highest bit to indicate the positive and negative signs of the symbol, then only three binary digits are left for the bit that can represent the value, and three binary digits indicate that the value can only represent eight.This is exactly how the original code represents an integer:
Figure 2
Figure 2As shown in. This representation seems natural in the "Regulations": the highest bit is the symbol bit, and the rest is the numerical bit. In fact, it is not:The bitwise pattern 1000 originally used to represent positive integer 8 is now used to represent-0. The bitwise pattern 1111 originally used to represent a positive integer 15 is now used to represent-7.If I say that only four binary digits are used to represent integers (positive and negative), 8 =>-0, 15 =>-7, 11 ==>-3 ,... (instead of telling you what the highest bit is used as the symbol bit, forget the rules of the symbol bit), will you feel a little "make no sense.
In the original code representation, 0 has two types of representation: 0000 and 1000, that is, + 0 and-0.
4. The second scheme indicating positive/negative numbers, anti-code
The idea is the same: Cut the dial and divide the dial by half. However, this half division method directly reverses the bit mode-each bit mode has an opposite bit mode, isn't it?:
Figure 3
Figure 3As shown in. The bitwise pattern 1111 originally used to represent a positive integer 15 is now used to represent-0.The bitwise pattern originally used to represent positive integer 8 is now used to represent-7. Similarly, in the anticode representation, 0 has two types of representation: 0000 and 1111, that is, + 0 and-0.
One's complement.Why is it called one's complement?
.If you use another method to indicate the inverse process:
We found that the operation to obtain the inverse of a number n is equivalent to using the number where all bits are 1 minus n.. Then let's look at the anti-code representation method, 1111What is the value of this bit mode?.Bit modeBitwise mode obtained after reverse RetrievalHow much does it represent? That is to say, if we use the one's complement notation, We can naturally (subtract through the bitwise mode) Get an equation like-0-5 =-5.This is something you cannot do with the original code representation! (For example, the-0 of the original code is 1000, and 5 is 0101,100 0-0101 = 0011, and 0011 indicates 3 in the original code ).However, since there are two representations of 0 in the anticode Representation Method: + 0 and-0, we can obtain the equation-0-5 =-5, but cannot get the equation of + 0-5 =-5.
5. indicates the final scheme of positive/negative numbers.
Figure 4
Figure 4As shown in, this is the complement representation. Originally used to represent a positive integer 15, the bitwise pattern 1111 is used to represent-1. Originally used to represent a positive integer 8, the bitwise pattern 1000 is currently used to represent-8. 0. There is only one representation, that is, 0000.That is, in this representation, 15 =>-1, 8 =>-8, 12 =>-4, this suddenly "make no sense? Why isn't the original code representation suddenly "make no sense"?... It's probably because of a certain nonsense "Theorem" that is easy to make people short-circuit...
Two's complement.The reason is very simple. Take + 5 as an example:
Indicates + 5, Bit modeIndicates. Bit mode(5 in total? It cannot be represented on our dialBut if we have to express it on the dial, I hope you can understand,(Imagine a hour hand, with a scale forward representing + 1. On this dial, it returns to zero when it reaches 16. actually % 16. (mod 16, mod 16 )).
In the complement representation, we naturally get 0-5 =-5 (Subtraction by bit mode. because there is only one method for representing 0 in the complement representation method, there is no problem with the + 0 and-0 of the anticode.
"The complement of a negative integer indicates that it is equal to the complement of the corresponding positive integer"This nonsense "Theorem ".
+ 5Is expressed, ThenIs expressed(~ Returns the bitwise inversion operator in C ).
1111 indicates-1 in the complement representation.-1 in the complement representation is obtained through the bitwise mode 0000-0001.(Ignore the borrowed digit, which cannot be expressed on the dial ).
This is the "Theorem.
Figure 5
Figure 5As shown in.Go(If we insist that half of the dial represents a negative number and half represents a positive number and uses a complement to represent), Or fromGo(If we think that all bit modes are used to represent positive numbers)., You can take two steps,Go counter-clockwise, OrClockwise Grid. If the values in the clockwise direction represent subtraction and addition, we have:
Which has only four binary digits)Complement Representation,Since the-4 and 12 bit modes are the same, the-3 and 13 bit modes are the same, and the-7 and 9 bit modes are the same, there is no difference between the two operations.. Actually The common mode is 16.
How does one explain whether a single-bit mode in memory is a positive number? Or is it a negative number? It's software.Of course, in the operation process, the computer will also perform carry/borrow Placement Based on the calculation results. the software can judge by the flag. because the expression of the complement code can simplify the design of the hardware circuit, the current computer represents the integer basically uses the complement representation.
If one Byte is used to represent an integer, the modulo is 2 ^ 8. If four bytes are used to represent an integer, the modulo is 2 ^ 32.In this case, you only need to draw a larger dial with a finer scale. The basic explanation is the same as that of only four digits indicating an integer.