Why do computers use two's complement data?

  • background
  1. The data stored by the user is divided into positive and negative numbers
  2. The highest bit is the sign bit. The high bit on the left and the low bit on the right. 0: Represents positive numbers, 1: Represents negative numbers
  3. original code (raw binary)
  4. one's complement
  5. complement
  • The original code
    +-1
    0000 0001 positive 1 binary
    1000 0001 negative 1 binary
    looks like there is no problem in storing it like this... Then list the sub

    +-0
    0000 0000 positive 0 Binary
    1000 0000 Negative 0 Binary comes to the

    conclusion by analysis:
    1.0 has two storage methods +1 ==== 0000 0001 -1 ==== 1000 0001 -2 ==== 1000 0010 the result is not right... so the one's complement was born....







  • Inverse code
    1. The sign bit remains unchanged on the basis of the original code, and the others are inverted (0 to 1, 1 to 0)
    2. The original code of the positive number is the same as the inverse code.

    Continue the original code of the above example
    :
    +1 ==== 0000 0001
    -1 ==== 1000 0001
    -2 ==== 1000 0010's complement

    :
    +1 ==== 0000 0001
    -1 ==== 1111 1110
    -0 ==== 1111 1111 The result is equal to -0. The problem caused by the addition of positive and negative numbers is solved by the inverse code. However, the two storage methods of 0 have not been solved yet......

  • Complement code
    1. The original code, complement code and complement code of positive numbers are the same
    2. The complement code of negative number is equal to the complement code + 1 complement

    code:
    +1 ==== 0000 0001
    -1 ==== 1111 1110

    's complement ( one byte storage ):
    +1 ====   0000 0001
    -1 ==== 1111 1111

     0 ==== 1 0000 0000 causes the highest bit to be discarded The actual result is: 0000 0000

Guess you like

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