The source code (true form) is a binary fixed-point representation of a number in a computer. The original code notation adds a sign bit (that is, the highest bit to the sign bit) before the value: a positive number is 0, a negative number is 1 (0 has two representations: +0 and-0), and the remaining bits represent the size of the value. The original code cannot participate directly in the operation and may be faulted. For example Mathematically, 1+ (-1) = 0, and 00000001+10000001=10000010 in binary, translates to decimal-2. There was obviously a mistake. Therefore, the symbol bit of the original code cannot be directly involved in the operation and must be separated from the other bits, which increases the cost and complexity of the hardware.
Anti-code is a kind of numerical storage, more applied to the system environment settings, such as the Linux platform directory and file default permissions set Umask, is the use of anti-code principle. In the computer, the fixed-point number has 3 kinds of notation: the original code, the inverse code and the complement.
The number of symbols in a computer has three representations, that is, the original code, the inverse code and the complement. The three representations have both symbolic and numeric digits, and the sign bit is 0 for "positive", 1 for "negative" and the value bit, and three for the method of representation.
In a computer system, the values are represented and stored in complement. The reason is that with the complement, the symbolic and numerical fields can be processed uniformly, and addition and subtraction can be processed uniformly. In addition, the complement and the original code are converted to each other, the operation process is the same, no need for additional hardware circuit.
Original code
(1) The original code: directly before the value of a symbol to add a bit of notation.
Example: Symbol bit value bit
[+7] Original = 0 0000111 b[-7] Original = 1 0000111 B
Attention:
The byte value range is -27~ 27-1 total 256 numbers (right)
That
Unsigned bit 0~255 (because the computer is calculated from 0 instead of 1)
Signed bit-128 ~ +127
Anti-code
(2) Anti-code: Positive number: The inverse code of positive number is the same as the original code. Negative number: The inverse of the negative, the sign bit is "1", the numeric part is reversed by a bit. Example: Symbol bit value bit
[+7] anti = 0 0000111 B
[-7] anti = 1 1111000 B
Note: 0000 B is not equal to 0 but-128
+127 +1 =-128
i.e. 0111 1111 b+1 = 0000 B
That is, a byte value overflow occurred.
8-bit binary inverse Code representation range: -127~+127
Why-128 of the binary will be 1000 0000;
1000 0000 (original) = 1111 1111 (reverse)
So the question is: 64+32+16+8+4+2+1 = 127 Why would there be 128?
The original negative anti-code is to be complementary, that is, in the final results +1
Note: There is only +0 in the computer and there is no 0 argument, because 0 is completely meaningless,
That is: only 0000 0000 = +0
and not 1000 0000 = 0
1000 0000 of the real identity is-128 (right)
Complement
(3) method of expression of complement
1) The concept of a module: a unit of measurement called modulus or modulus. For example, the clock is counted as a 12-based loop, which is modulo 12. On the clock, the hour hand is added (positive dial) to the integer digits of 12 or minus (reversed) 12 of the integer digits, the position of the hour hand is constant. 14 O'Clock after the Die 12, become (PM) 2 o'clock (14=14-12=2). From 0 to 10, minus 10 hours, it can also be seen from 0 points clockwise Dial 2 (plus 2 hours), that is, 2 points (0-10=-10=-10+12=2). Therefore, under the premise of modulo 12, 10 can be mapped to +2. Thus, for a circulatory system with a modulus of 12, the effect of adding 2 and minus 10 is the same; therefore, in 12-mode systems, all operations minus 10 can be replaced by the addition of two, which translates the subtraction problem into an additive problem (note: The computer's hardware structure is only adder, So most of the operations must eventually be converted to addition. 10 and 2 are complementary to modulo 12.
Similarly, the computer's operating parts and registers have a certain word length limit (assuming the word length is 8), so its operation is also a modulo operation. When the counter is full of 8 bits, which is 256 digits, an overflow is generated and counted from the beginning. The amount of overflow generated is the module of the counter, obviously, the 8-bit binary number, its modulus is 2^8=256. In the calculation, two complementary numbers are called "complement".
2) The expression of complement:
Positive number: The complement of positive numbers is the same as the original code.
Negative number: The complement of a negative number is the sign bit "1". And, this "1" is both the sign bit and the value bit. The numeric part is reversed and then the bottom (lowest bit) is added by 1. That is, "anti-code +1."
Example: Symbol bit value bit
[+7] complement = 0 0000111 B
[-7] complement = 1 1111001 B
The complement is an important form of coding in a microcomputer, please note:
A. After using the complement, the subtraction operation can be easily converted into addition operation, and the operation process is simplified. A positive complement is the true value of the number it represents, and the complement of a negative number part is not the truth of the numbers it represents. The results are still in complement with the complement operation.
B. Unlike the original code, the inverse code, the value of 0 of the complement only one, that is, [0] complement =00000000b.
C. If the word length is 8 bits, then the complement is expressed in the range of -128~+127; When doing the complement operation, it should be noted that the result should not exceed the range of the number of the complement can be expressed.
Transformation
Edit
Because the positive number of the original code, complement, anti-code representation method are the same, do not need to convert.
In this case, only a negative number is analyzed.
( 1 ) The original code is known, and the complement is obtained.
Example: It is known that a number of X's original code is 10110100B, try to find X's complement and anti-code.
Solution: by [x] Original =10110100b known, X is negative. In order to find the inverse code, the sign bit is unchanged, the numerical part is reversed by the position, and when the complement is obtained, it is added 1 in its inverse code.
1 0 1 1 0 1 0 0 Original code
1 1 0 0 1 0 1 1 inverse code, symbol bit invariant, value bit reversed
1 +1
1 1 0 0 1 1 00 complement
Therefore: [X] complement =11001100b,[x] anti-=11001011b.
( 2 ) known complement, seek the original code.
Analysis: According to the inverse process of seeking negative complement, the value part should be the lowest minus 1, and then reverse. However, for the binary number, the first minus 1 after the inverse and first take the inverse plus 1 to get the same result is the same, it can still be used to take back plus 1 has a method.
Example: A number of X's complement 11101110B is known, try to find its original code.
Solution: by [x] complement =11101110b know, X is negative.
Using Inverse push method
1 1 1 0 1 1 1 0 complement
1 1 1 0 1 1 0 1 anti-code (minus 1)
1 0 0 1 0 0 1 0 Original code (symbol bit invariant, value bit reversed)
Example
Edit
Please do two questions: overflow problem with signed number arithmetic
How do two positive additions become negative???
1) (+72) + (+98) =?
0 1 0 0 1 0 0 0 B +72
0 1 1 0 0 0 1 0 B +98
1 0 1 0 1 0 1 0 B-86
How do you get a positive number when you add two negative numbers???
2) (-83) + (-80) =?
1 0 1 0 1 1 0 1 B-83
1 0 1 1 0 0 0 0 B-80
0 1 0 1 1 1 0 1 B +93
Thinking: These two questions are calculated according to the normal rules, but the results are obviously incorrect.
Answer: This is because an overflow occurred.
If the computer's word length is n-bit, the highest bit of n-bit binary is the sign bit, the rest of the n-1 bit is the numeric bit, and when the complement notation is used, the range of the number X that can be represented is-2 of the n-1 power ≤x≤2 n-1 Power-1
When N=8, the range of signed numbers that can be represented is -128~+127. When two signed numbers are additive, an overflow occurs when the result of the operation exceeds the range of the signed number that can be represented, resulting in an error in the calculation. Obviously, overflow can only occur in cases where two of the same symbol number is added or two different symbol numbers are subtracted from each other.
For addition operations, overflow occurs if the secondary high (the value portion of the highest bit) forms a carry join the highest bit, and the highest bit (sign bit) is added (including the secondary high carry) without carrying the output, or conversely, the secondary high has no rounding to the highest bit, but the highest bit has a carry output. Because these two cases are: two positive numbers are added, the result is out of range, the form becomes negative, the two negative numbers are added, the result is out of range, the form becomes a positive number.
And for the subtraction operation, when the secondary high does not need to borrow from the highest bit, but the highest bit is required borrow (positive offloading number, the difference out of range), or conversely, the secondary high must be from the highest bit borrow, but the highest bit does not need to borrow (negative number minus positive, the difference out of range), also overflow.
In the computer, the data is stored in the form of complement, so the complement in the C language teaching has a relatively important position, and the interpretation of the complement must involve the original code, anti-code.
In the number of machines in n-bit, the highest bit is the sign bit, the bit is zero for positive, one is negative, the remaining n-1 bits are numeric bits, and the values of the others are zero or one. When the value is positive, the original code, anti-code, and the complement of the value of the exact same value, if it is negative, the value of the original code is kept as is, the value of the inverse code is the value of the original code number of the values of the counter, complement is the lowest bit of the inverse code plus one. Note that the symbol bit is not changed.
Original code, anti-code, complement