The source code, the anti-code, the re-code, and the reason for using the anti-code and the re-code.
The original blog website has no intention of infringement, but it is convenient to remember it for later review,
Http://www.cnblogs.com/zhangziqiu/archive/2011/03/30/ComputerCode.html
Machine count
The binary representation of a number in a computer is 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, in decimal format, if the computer word length is 8 bits, the conversion to binary is 0000 0011, and if it is-3, it is 1000 0011. in this case, 0000, 0011, and 1000 are the number of machines,
True Value
Because the first digit is the symbol bit, the formal value of the number of machines is not equal to the real value. For example, the above signed number is 1000 0011, and the highest bit 1 represents a negative value. The real value is-3, instead of the form value 131 (1000 0011 is converted to a 10-digit value equal to 131), the real value corresponding to the signed number of machines is called the true value of the number of machines for the sake of difference. For example, the true value of 0000 0001 = + 000 0001 = + 0001 0001 =-000 =-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 bits represent the value. For example, if it is an 8-bit binary, [+ 1] original = 0000 0001. [-1] original = 1000 0001. because the first digit is the symbol bit, the value range of the 8-bit binary is [1111 1111,0111 1111], that is, [-127,127]. The original code is the easiest way for the human brain to understand and calculate.
Anti-code:
The expression of the anticode is as follows: the anticode of a positive number is itself, and the anticode of a negative number is based on the original code. The symbol bit remains unchanged, and the other bits are reversed, [+ 1] = [0000 0001] original = [0000 0001] Reverse, [-1] = [1000 0001] original = [1111 1110] reverse. It can be seen that if an anticode represents a negative number, the human brain cannot intuitively see its value. Generally, it needs to be converted to the original code and then computed.
Complement:
The complement expression is as follows: the binary complement of a positive integer is the same as the Binary source code of the negative integer. First, calculate the binary code of the positive integer corresponding to the negative number, and then add 1 to all digits, fill 1 on the left when the number of digits is not enough. For example, [+ 1] = [0000 0001] original = [0000 0001] reversed = [0000 0001, [-1] = [1000 0001] original = [1111 1110] inverse = [1111 1111] Complement. For negative numbers, the human brain cannot intuitively see the value of the complement representation, it is also usually necessary to convert to the original code and then calculate its value.
Why do I need to use the original code to complete the code,
Now we know that the computer can use the three encoding methods of the original code to represent a number. For positive numbers, because the three encoding methods are the same, there is no good explanation, but for negative numbers, since the original code is directly recognized by the human brain and used for computation, why do we need to use the inverse code and complement code? First, because the human brain can know that the first digit of the original code is the symbol bit, During computation, We will select the addition and subtraction of the true value area based on the symbol bit. However, for computers, addition, subtraction, multiplication, division, and other operations are the most basic operations, the design should be as simple as possible. Computer identification of the symbol bit will obviously make the computer's basic circuit design very complicated. So people come up with a method to take the symbol bit into operation. We know, according to the algorithm, if a positive number is subtracted, a negative number is added, that is, 1-1 = 1 + (-1) = 0. Therefore, the machine can only add but not subtract, in this way, the design of computer operations is simpler. If the original code is used for calculation, 1-1 = 1 + (-1) = [0000 0001] original + [1000 0001] original = [1000 0010] original =-2. if the original code is used for calculation, the symbol bit will also be involved in the calculation, Obviously, the result of subtraction is incorrect. That is why the computer does not use the original code to represent a number. In order to solve the problem of the original code subtraction, there is an inverse code. If we use the inverse code to calculate and subtract, 1-1 = 1 + (-1) = [0000 0001] original + [1000 0001] original = [0000 0001] reversed + [1111 1110 reversed] = [1111 1111 1000] reversed = [0000] original =-0, it is found that the negative code is used to calculate the subtraction, and the true value of the result is correct, and the only problem actually occurs in the special value 0, although it is understood that + 0 is the same as-0, 0 is meaningless, and there will be two [0000 0000] original and [1000 0000] original encodings that represent 0; the occurrence of the complement solves the problem of the zero sign 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 case, [0000 000 0] indicates that the previous problem-0 does not exist, and [1000 0000] indicates-128; (-1) + (-127) = [1000 0001] original + [1111 1111] original = [1111 1111] supplement + [1000 0001] Supplement = [1000 0000. -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. in addition, you can save one minimum value when using the complement representation.