[0001] Wang techniques machine number, true value, the original code, anti-code complement summary

First, the concept list

  1. Machine number

  2. True value

  3. Original code

  4. Inverted

  5. Complement

Second, the concept Detailed

1. The number of machines

  1. Machine number is a binary representation of the number in the computer.

  2. Machine signed number, most significant bit is stored, n is 0, a negative number.

    十进制数+3,计算机字长为8位的话,转换二进制为00000011
    十进制数-3,计算机字长为8位的话,转换二进制为10000011
    上述这两个二进制就是机器数。

2. The true value

  1. Due to the presence of the sign bit, the true value is not equal to the number of the machine.

  2. Signed number of machines, the real value.

    -3是真值,它的机器数是10000011(其形式值为131(十进制))

3. The original code

  1. Original code is the sign bit plus the absolute value of the true value, i.e. the first bit is the sign bit, the remaining bits representative value.

        十进制数+1原码为 00000001
        十进制数-1原码为 10000001
    
  2. Because the first one is the sign bit, so the 8-bit binary value ranges:

    [11111111,01111111]

    which is

    
        [-127,127]    
        
  3. Original code of the human brain is the most easily understood and calculated encoding.

  4. Unsigned with the original code.

4. Anti-code

  1. Positive anti-code is its own.

  2. Negative anti-code is the original code, based on the sign bit unchanged, the rest of you negated.

        [+1] = [00000001]【原】 = [00000001]【反】
        [-1] = [10000001]【原】 = [11111110]【反】
  3. Anti-code is not easily seen directly the true value of the human brain, usually need to be converted into the original code

5. Complement

  1. The complement is a positive number in itself.

  2. Negative complement is the basis of its original code on the sign bit unchanged, the rest of you negated, finally +1.

        [+1] = [00000001]【原】 = [00000001]【反】 = [00000001]【补】
        [-1] = [10000001]【原】 = [11111110]【反】 = [11111111]【补】
  3. Complement is not easy to be seen directly the true value of the human brain, usually need to be converted into the original code

  4. The computer, using a signed number complement

Third, the number of symbols have meaning and method of using complementary codes

  1. Meaning: In order to participate in the sign bit operations to simplify circuit design basis.

    The human brain can know first is the sign bit, when we calculated based on the sign bit, select the subtraction of the true values ​​of the region. (The concept of true value most in the beginning of this article), but for a computer, plus or minus multiplier has It is the most basic operation, designed to be as simple as possible. computer distinguish "sign bit" to make clear that the basis of computer circuit design becomes very complicated! so people came up with the sign bit is also involved in computing. we know, based on the calculation rule subtracting a positive number equal to plus one negative, namely: 1-1 = 1 + (-1) = 0, so that the machine can not be in the subtraction-addition, such computer operation is even simpler design, people began to explore involved in computing the sign bit, and to retain only the addition of methods.

  2. method:

    For example: is defined as 8-bit binary, decimal calculation expression: 0 = 1-1

    • If the code is calculated using the original machine code, such a result would be:

      
          1-1 = 1 + (-1)= [0000 0001]【原】+[1000 0001]【原】= [1000 0010]【原】= -2【十进制】 //明显错误 
    • If the machine code is calculated using the inverse code, the result would be something like:

          1-1 = 1 + (-1)= [0000 0001]【原】+[1000 0001]【原】= [0000 0001]【反】+[1111 1110]【反】= [1111 1111]【反】= [1000 0000]【原】= -0 【十进制】 //明显错误,0应该是唯一的,不能让[0000 0000]【+0】[1000 0000]【-0】都来表示0. 
    • If the machine code's complement is calculated, the result would be something like:

          1-1 = 1 + (-1)= [0000 0001]【原】+[1000 0001]【原】= [0000 0001]【反】+[1111 1110]【反】= [0000 0001]【补】+[1111 1111]【补】= [0000 0000]【补】= [0000 0000]【原】= 0 【十进制】
          //这样保证了0的唯一性。
      
      
      还有一个额外的好处,若用原码、反码,取值范围都只限与[-127,127],但用补码是[-128,127]能多表示一个数-128。
      
          (-1)+(-127)= [1000 0001]【原】+[1111 1111]【原】= [1111 1110]【反】+ [1000 0000]【反】= [1111 1111]【补】+[1000 0001]【补】= [1000 0000]【补】
          //注意:-128在限定为8位时没有原码和反码,不信?那你将[1000 0000]【补】往原码进行转换,会得出[0000 0000]【原】,这明显错误!
          
      若机器采用补码表示有符号数,对于编程中常用32位的int类型,表数范围可以是[-2<sup>31</sup> , 2<sup>31</sup>-1],而不是原码的表数范围[-2<sup>31</sup>-1 , 2<sup>31</sup>-1] 

Fourth, how will revert to the original complement binary code?

  1. Method a: we can backtrack, is stored in the computer's complement binary minus 1, then negated, then get the original code, into a corresponding negative can be, but this is troublesome because it involves the subtraction operation.

  2. Method 2: complement negative first negated, then add 1, 1 can be changed to the highest position.

    For -10, in the memory of a computer

    1,111,111,111,111,111 1,111,111,111,110,110 complement form []

    First negated, too

    00000000 00000000 00000000 00001001

    Plus one, too

    00000000 00000000 00000000 00001010

    1 becomes the most significant bit, i.e.,

    1,000,000,000,000,000 0,000,000,000,001,010 [true form]

    The result is "-10"

Five, clever but useless

1. Take a shortcut method of the inverse of

It is equivalent to ~ x - (x + 1)


10进制数2016,转32位2进制数为:

0000 0111 1110 0000

~按位取反:

1111 1000 0001 1111 【对应十进制数:-2017】 结果为: (~2016) = -2017 

VI. References

  1. The original code, anti-code complement Detailed  [this great God is too NB, pull the mathematical knowledge came, I do not understand some, yet the contents are incorporated herein, the follow-up to see to understand coming up]

  2. Bitwise Operators ~ - Shi Shuo - blog Park

Guess you like

Origin www.cnblogs.com/lanqingzhou/p/11888935.html