之前对计算机表示数据的方式一直很含糊。这次通过一篇外网的blog来重新理解一下
Numerical representation in computer
Any data exists in the form of binary machine code in a computer. For example, +2 in real life is represented as 00000010 in a machine (let’s assume that the word length of this computer is defined as 8 bits). But this is the machine code corresponding to + 2, because the first is used to represent the positive and negative values: 0 is positive and 1 is negative. So if you want to express – 2, the corresponding machine code is 10000010, so when you see a series of machine codes, you should first consider the positive and negative values. It is wrong to calculate the true value of 10000010 directly by weight expansion, if it is directly by weight expansion.12^7 + 12^1 = 130 Obviously not 2. Because the first bit is 1, it is negative, and then the weight expansion of the latter bit is calculated to be -2.
计算机中保存任何数据都以用二进制机器码的形式。假设在一个8bit位长机器上,00000010
表示的是+2
,二进制数的首位是符号位,0代表正数,1代表负数,所以10000010
代表-2
。
Original code
As an example in the previous paragraph, the value represented by machine code is called the original code, the original code of + 2 is 00000010, and the original code of – 2 is 10000010.
原码就是直接将数字以二进制表现出来的形式,同上所述。
Inverse code
Inverse code is generally the least useful representation of computer. Why do we talk about it a little? First, how to express inverse code: if it is positive, it is the original code itself, and there is no change. If it is the inverse code of negative numbers, then the number of the sign bits, that is, the first bit, remains unchanged, and the rest of the bits are inversed. For example, if the original code of + 2 is 00000010, then the inverse code of + 2 is 00000010. – The original code of 2 is 10000010, and the inverse code is 11111101.
反码在计算机表示数据中作用不大。反码的表示方式:如果原码表示的是正数,那么它的反码和原码相同。如果原码表示的是负数,那么它的反码是保留符号位不变,其余各位取反。-2
的原码是10000010
,那么它的反码是11111101
Complement code
Complement is one of the most frequently used encoding methods in computer numerical operations. The specific reasons are discussed a little. First, let’s talk about the representation of complement. The positive complement is the original code itself, which is the same as the inverse code. The complement of negative number is the result of its inverse code + 1. For example, the complement of + 2 is 00000010. The complement of – 2: 11111111101 (counter code) +1 = 11111111110.
补码在计算机数值系统的编码方法中使用最多。补码的表示方式:正数的补码和它的原码相同。负数的补码是它的反码+1。-2
的补码是11111101+1=11111110
注:0
的补码是00000000B
Frameshift
Shift code is to add a bias constant to the value. When the number of coding bits is n, it usually shifts to the N-1 power of 2. For example, the shift code of – 2 = the 7th power of the original code + 2 = 00000010 + 10000000 = 10000010, which can be used as the complement of – 2 11111110 Sign bits remain unchanged and the remaining bits are reversed + 1。
这段没太看懂,如果是移码概念的话和百科里说的不一样,如果有大佬知道请评论区指导。
个人理解。-2 = 2 + 2^(n-1)//n为字长位数 = 00000010 + 10000000 = 10000010 = -2的补码,符号位不变,其余各位取反+1(11111110 -> 10000010)
。看起来好像是正数和负数的转变。
相当于一个数的移码就是这个数的绝对值加上2的(位长-1)次方。
Why are there complements and countercodes?
Modular operation system
Take clocks and watches in life. If 10 o’clock becomes 6 o’clock, you can call back 4, 10-4 = 6. You can also dial 8 clockwise, 10 + 8 = 18 modulus 12 = 6. It can be seen that for module 12, – 4 and + 8 are equal. 8 is the complement of 4 for module 12. In this way, all subtraction calculations can be converted into addition operations.
If it is a “4-bit decimal” module system operation, 9828-1928 operation is equivalent to 9828 + (10 ^ 4-1928) = 9828 + 8072 = 17900, take 10 ^ 4 module = 7900. So when subtracting in computer, it is equivalent to adding – 1928 complement.
In fact, it is the most intuitive and simple for our brain to use the original code directly for numerical calculation. Why can’t computers use the original code directly for numerical calculation, because there is a very important operational factor – negative number. We know that the first bit of the original code is the symbol bit 0 for the positive number 1 for the negative number, but the computer does not know. If 1+(-1) is calculated, the result is 00000001 (+1 primitive code) +10000001 (-1 primitive code) = 10000010 (primitive code) =-2.Obviously the result is wrong.
So does the birth of inverse code solve this kind of problem?
Come and try. 1+(-1) = 00000001 (original) + 10000001 (original) = 00000001 (negative) + 11111110 (negative) = 11111111111 (negative) = 10000000 (original) = - 0. Notice here 0 is a signed bit and a negative value。 This is inconsistent with our mathematical operation rules (although zero is positive and negative in reality, the correct result in the computer should be + 0)
At last, the encoding method of complement code is used to compute. The result is correct: 1+(-1) = 00000001 (original) + 10000001 (original) = 00000001 (inverse) +111111110 (inverse) = 00000001 (complement) + 11111111 (complement) = (0)00000000 (complement) = 0.
补码的意义:模操作系统
时钟举例(没见过别的例子,确实这个好理解吧)。从10点拨到6点,可以往回拨4个小时,10-4=6
,也可以从后拨8个小时,10+8=18,18%12=6
。对于模12来说,-4和8相同那么8就是-4在模12系统中的补码。这种情况下所有的减法操作都可以用加法操作来代替。
在4位的10进制模系统操作中,9828-1928=9828+(10000-1928)=9828+8072=17000,17900%1000=7900
。在取模系统中,进制数的位长次方即10^4
位模。
原码解决不了1+(-1)=0
的问题,反码的结果是-0
,对于计算机来说应该符合数学事实即+0
,最后是补码解决了这个问题。
Why is there a code shift?
Code-shifting is mainly introduced for the convenience of computer operation index. That’s one more addition than we do. 1.12^-1 + 1.22^3 According to the operation rules, when the base number 2 is the same, the addition of 1.1 and 1.2 can be performed only when the exponents are treated as the same.0.000112^3 + 1.22^3 This makes the operation much simpler. Therefore, in order to perform the operation, we need to compare the exponential part. If the exponential-1 and 3 are compared by the original code coding, the results of the comparison between 10000001 and 00000011 will be formed. Obviously, the ratio is – 1 > 3, and the computer can not perform the accurate operation. If we use the form of shift code, for example, let the exponent-1 and 3 + 4 at the same time, it will be a comparison between 3 and 7. It is easy to compare the size of the comparison, and it is easy to align the maximum power for calculation. So the idea of code shifting comes from this.
移码(?)的来由。在做浮点数的加减法时,如果底数的指数部分次方相同,可以简化计算。但是直接用次方的原码比较,负数大于正数,此时没办法将指数部分小的浮点数部分正确移位(即小数点向前移增加指数)。所以可以通过移码将两个数的指数加一个值来做比较,也可以确定应该对浮点数的小数点移多少位(对指数做减法)