From: http://icebloods.blog.sohu.com/64118412.html
To facilitate the description, we first introduce two terms: machine number and true value. The representation of a number in a machine, that is, the encoding is called the number of machines, and the number itself is called the true value. There are three common machine numbers: source code, complement code, and anti-code.
1. Original code
1) general definition
Digitize the number symbol, that is, use a binary digit to represent the symbol: to positive number, this bit is taken 0, to negative number, this bit is taken 1. While the value part maintains the original form of the number (sometimes you need to add a few zeros to the high part ). The result is the original code of the number.
For example, x = + 1001010, y =-1001010, Z =-1110 (=-0001110 ). When the original code is 8 bits, the original codes of X, Y, and Z are:
[X] original = 01001010;
[Y] original = 11001010;
[Z] source = 10001110.
The highest bit is the symbol bit.
2) regular definition
X indicates the true value, and N indicates the number of digits of the original code. This definition actually gives the range of the true value. When n = 8,-127 <x <127. Therefore, the value part of the value is written in binary format, and the maximum value is 7 bits. From this definition, we can see that when X is a positive number, its original code is still the number itself. when X is a negative number, the value of-X equals to removing the negative number, but add 127, that is, 8th bits for 1 (127 = 10000000 (2 )). Therefore, this definition is consistent with the general definition above.
3) features of original code representation
The original code representation has three main features: intuitive, easy to convert to the true value, convenient multiplication and division operations, and complicated addition and subtraction operations. The first point is obvious. The original code indicates that it is convenient to perform multiplication and division operations because the numeric part maintains the original form of the data, and the numeric part can be multiplied or divided to obtain the product or quotient value, the sign bit of the product or commodity can be obtained by logical calculation of the sign bit of two original codes. It is difficult to add or subtract the original code. In addition, for example, to add two numbers, you must first identify the symbol bit. If the two numbers are different, you must actually perform subtraction. Then, you must determine the absolute value, use a number with a large absolute value to Reduce the number with a small absolute value, and finally determine the signed bit of the result.
2. Supplemental code
1) Introduction and definition of supplemental code
According to statistics, in all operations, addition and subtraction operations account for more than 80%. Therefore, whether positive and negative addition and subtraction operations are convenient is directly related to the operation efficiency of computers.
For example, an example in daily life is a pointer clock. Now, when the hour hand points to six o'clock, there are two ways to direct it to. One is to dial 7 cells, and the other is to reverse dial 5 cells. If the clock is regarded as a calculator, the positive dial is regarded as a plus operation, and the reverse dial is regarded as a minus operation, there are: 11-5 = 11 + 7 on the clock, that is, 11 + (-5) = 11 + 7. This is because 11 + 7 = 12 + 6, and 12 is equivalent to 0 on the clock. When it exceeds 12, 12 is lost. This operation is called a modulo operation. The clock model is 12. The so-called "modulo" refers to the range of a system, or the maximum number that a system can represent (to be exact, add 1 to the maximum number ). A modulo operation means that the modulo is lost when the operation result exceeds the modulo. When the modulo is an integer, the modulo operation can also be understood as the process of dividing by the Modulo for the remainder. The commonly used symbol "Mod" indicates a modulo operation.
In a computer, the number of digits of a specific data type is determined. For example, if the byte data is 8 bits, when each bit is 1, plus 1, the highest bit will generate carry. If no action is taken, the carry is lost, and the loss is 2 ^ 8 = 256. This is the 8-bit data model. As shown in the preceding example, by modulo operation, positive and negative numbers can be converted into positive and positive numbers (11-5 = 11 + 7 ), A negative number can be equivalent to a positive number (-5 is equivalent to + 7 ). Check the computer information. For example, if you want to add + 0001111 (15) and-0001100 (-12), you actually need to perform subtraction, but instead of doing so, you need to first add-0001100 and the modulo 10000000 (256) add, get 11110100 (-12 + 256 = 244), and then add the original number 0001111 to it (11110100), get 00000011 (15 + 244 = 256 + 3 = 3 ), the carry of the highest bits, that is, loss of modulus.
In this way, the correct result is also obtained. It can be seen that in a computer, as long as a negative number is added to the modulo, it can be converted into a positive number, so that a positive number plus a negative number is converted into a positive number plus a positive number.
The result of Modulo a negative number is called the complement code of the negative number (the result is a positive number, which is equivalent to the negative number, and is exactly one-to-one, therefore, it can be regarded as the encoding of the negative number. The complement code that defines a positive number is itself, and the sign bit is 0, that is, the same as the original code. This is the general definition of the complement code. If this definition is expressed in mathematical form, the formal definition of the complement code can be obtained:
Where N is the number of digits of the complement code. This definition actually gives the range of true values. When n = 8,-127 ≤ x <127. Compared with the original code, the complement code can represent more than one number. When n = 8, the multi-representation is-127 =-128.
2) method for completing code
For positive numbers, the complement code is the same as the original code. For example, x = + 0101001, [x] fill = [x] original = 00101001. For negative numbers, decode by definition. Subtraction is required, which is inconvenient. According to the derivation, the complement of a negative number is equal to the bitwise "inverse" (1 to 0, 0 to 1) except the symbol bit, and the last plus 1. For example, y =-0001100, [y] original = 10001100, [y] fill = 11110011 + 1 = 11110100.
If you do more, you can find a method to calculate the compensation. The value of the first 1 from the inner bit remains unchanged, and the symbol bit remains unchanged, this method is only used to solve the problem ).
From the method of complement, we can see that for the complement code, its symbol bit is the same as the symbol box of the original code, and it also represents the true value symbol.
3) nature of the complement
① [X + y] fill = [x] fill + [y] fill, that is, the complement of the sum of the two numbers equals the sum of their complement codes.
② [X-y] fill = [x] fill + [-y] fill, that is, the complement of the difference between two numbers is equal to the sum of the complement of the subtrahend and the complement of the opposite number of subtrahend.
③ [[X] fill = [x] original, that is, based on the Complement Method, perform a supplement for [X], and the result is equal to [X] original.
4) use the complement code for addition and subtraction operations
The purpose of introducing the complement code is to facilitate the addition and subtraction of signed numbers. Here we only look at addition. Here are several examples. Note: The symbol bit is also involved in the operation. The carry of the symbol is a modulo and should be discarded. The following are three examples of summation using the complement code. Assume that the number of machines is 8 bits.
The sign bit is 0 and the true value is positive. Therefore, X + Y, = + 0001011 (2) (+ 11 ).
The symbol is automatically lost because it has a forward position. The sign bit is 1, and the true value is negative. After [x + y] is supplemented, [x + y] is obtained from the original value = 10001011. Therefore, x + y =-0001011 (-11 ).
The symbol is automatically lost because it has a forward position. The symbol bit is O, and the true value is positive. Therefore, x + y = + 0000111 (+ 7 ).
The above is a complement operation of three types of numbers: positive number + positive number, negative number + negative number, positive number + negative number. Negative number + positive number + negative number is the same. It can be seen that the number addition operation can be unified into the complement addition operation of the number of incoming lines.
The above example is only used to illustrate the benefits of introducing the complement code. The calculation process is not the actual process of the computer. In a computer, you do not need to convert the source code and the complement code every time. You can store the calculation results in the form of a complement code to directly participate in subsequent operations.
3. Anti-code Introduction
The inverse code of a positive number is the same as the original code and the complement code. For negative numbers, the anticode is equal to the original code except the symbol bit. It is reversed by bit (the last digit does not add 1 ). The use of anticode can also convert the addition and subtraction of signed numbers into pure addition, but it is troublesome. Generally, the reverse code is used as the intermediate process for completing, that is, [x] Complement = [x] Reverse + 1.
The machine number code described above is mainly used for assembly language programming. In advanced languages, numbers can contain symbols, but the compiler still expresses the number of machines.
Slowgrace note: In VB, a negative number is also indicated by a complement code. For example, do the following test in the immediate window:
? Hex (clng (-1 ))
Ffffffff