Computer source code, inverse code, complement code

The source code doesn't need to be said, let's talk about the complement first,

Why say complement first?

Because the one's complement is generated by the complement, see the following:

In computer systems, values ​​are always represented and stored in two's complement numbers. The reason is that, using the complement code, the sign bit and the value field can be processed uniformly; at the same time, addition and subtraction can also be processed uniformly. In addition, the complementary code and the original code are converted to each other, and the operation process is the same, and no additional hardware circuit is required.

characteristic


1. Add a negative integer (or original code) to its complement (or complement), and the sum is modulo.
2. Find the complement of an integer, which is equal to the integer itself.
3. The positive and negative zero representations of the complement code are the same.
touch
The concept of modulo can help understand two's complement and two's complement.
"Modul" refers to the counting range of a metering system. such as clocks. A computer can also be regarded as a measuring machine, which also has a measuring range, that is, there is a "module". E.g:
The measurement range of the clock is 0 to 11, modulo=12. The computer measurement range representing n bits is 0~2^(n)-1, modulo=2^(n).
"Module" is essentially the amount of "overflow" produced by the meter, and its value cannot be represented on the meter, only the remainder of the modulus can be represented on the meter. Any modulo meter can convert subtraction into addition .
For example: Assuming that the current hour hand points to 10 o'clock and the exact time is 6 o'clock, there are two ways to adjust the time: one is to dial back 4 hours, that is: 10-4=6; the other is to dial 8 hours forward : 10+8=12+6=6
In a system with modulo 12, the effect of adding 8 and subtracting 4 is the same, so any operation of subtracting 4 can be replaced by adding 8. For "modulo", 8 and 4 are complements of each other. In fact, in a 12-mode system, 11 and 1, 10 and 2, 9 and 3, 7 and 5, and 6 and 6 have this feature. The common feature is that the sum of the two is equal to the modulus.
For computers, the concepts and methods are exactly the same. An n-bit computer, set n=8, the maximum number that can be represented is 11111111. If you add 1 to it, it becomes 100000000 (9 bits), but because there are only 8 bits, the highest bit 1 is naturally lost. This returns 00000000, so the modulo 2^8 for an 8-bit binary system. In such a system, the subtraction problem can also be transformed into an addition problem, just by expressing the subtrahend with the corresponding complement . Complement numbers are used in the processing of computer logarithms, that is, complement numbers.
Two other concepts: ()
(1): ones' complement: In this representation, positive numbers remain unchanged (because this scheme is to solve the operation of effectively turning subtraction into negative addition, so positive numbers do not need to be changed, the inverse here, It is relative to the binary form of positive numbers), and negative numbers use the formula
   
[1]    (n is the number of bits including the sign bit) calculation. You can visually invert each bit of the binary form of the corresponding positive number. (This is not the same as the steps to get the inverse code. The inverse code and the complement code are obtained from the original code. Here is the operation from the positive number. However, except for the different starting points of the calculation, the final encoding form is Such. In order to distinguish the difference of the operation process, the English name is still used.)
Take a four-digit binary number as an example. Then each bit of the binary (0111) of -7 is inverted to get its ones complement number (1000). That is, the leftmost bit is the sign bit. N=7, n=4, bring into the formula, get
   
=-7 and its binary form, the process is as follows:
2^{4}= 10000
minus 1 - 0001
01111
Minus 7 - 0111
This part of 0 1000 in bold is rendered as -7 .
The top is to pave the way for the bottom
(二):twos complement:由于上面一种表示法
   
[1]   (n为将符号位算在内的位数)
观察公式,twos complement数,相当于ones complement 数+1.
下面用4位二进制数来做例子:
2^{4}= 10000 2^{4}= 10000
加1  01001
01001加粗部分表示-7 0 0111 加粗部分表示+7

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324669321&siteId=291194637