Original code, inverse code, complement code

What is the original code?

      The original code is an early way of representing numbers: a positive number, converted into binary bits is the original code of the positive number. Converting the absolute value of a negative number into binary bits and then adding 1 to the high bit is the original code of the negative number. To put it more simply, the original code of the negative number is the original code of the corresponding positive number whose high bit is 1.

      for example:

      The original code of 3 of type int is 11B (B means binary bit), which occupies four bytes on a 32-bit machine, so the high-order zeros must be filled:

      00000000 00000000 00000000 00000011

      The binary bit of the absolute value of -3 of the int type is the above 11B expansion and then the high-order zeros are filled:

      10000000 00000000 00000000 00000011      

      But the original code has several shortcomings, zero points are two kinds of +0 and -0. Weird isn't it! Also, when performing addition operations with different signs or subtraction operations with the same sign, it is not possible to directly determine whether the result is positive or negative. You need to compare the absolute values ​​of the two values, then perform addition and subtraction operations, and finally the sign bit is determined by the larger absolute value. So the inverse code is generated.

    

    What is the inverse code?

      The inverse code of a positive number is the original code, and the inverse code of a negative number is equal to the inversion of all bits of the original code except the sign bit.

      for example:

      The complement of 3 of type int is

      00000000 00000000 00000000 00000011

      Nothing to say like the original

      The complement of -3 of type int is

      11111111 11111111 11111111 11111100

      Invert all bits except the sign bit

      Solved the problem of addition and subtraction, but there are still positive and negative zeros, and then it's the complement

    

    What is complement?

      The complement of a positive number is the same as the original code, and the complement of a negative number is the inversion of all bits of the original code except the sign bit (the inverse code is obtained), and then the lowest bit is added by 1.

      Or give an example:

      The 3's complement of an int is:

      00000000 00000000 00000000 00000011

      -3's complement of int type is

      11111111 11111111 1111111 11111101

      is its inverse code plus 1

 

Finally to sum up:

    The complement and complement of positive numbers are the same as the original code.

    The inverse code of a negative number is the inversion of the original code of the number except the sign bit.

    The complement of a negative number is the inversion of the original code of the number except the sign bit, and then add 1 to the last bit  

 

    

Advantages and disadvantages of each:

    The original code is best to understand, but addition and subtraction are not convenient enough, and there are two zeros. .

    The inverse code is slightly more difficult, solving the problem of addition and subtraction, but there is still a zero

    It is difficult to understand the complement code, and there are no other shortcomings.

Guess you like

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