Correctly understand binary original code, inverse code, complement operation_01

Original code: The computer can only recognize 0 and 1, and the binary is used. The numerical value is divided into positive and negative, and the computer uses the highest bit of a number to store the sign (0 is positive, 1 is negative), which is the original code of the machine number.

The following examples all assume a word length of 8 bits.

If the original code is used to calculate:

    (1) + (1) //Original code calculation

=(0000 0001) + (0000 0001)

=(0000 0010)

=(2) //The result is correct


    (1) - (1) //Original code calculation

=(1) + (-1)

=(0000 0001) + (1000 0001)

=(1000 0010)

=(-2) // wrong result


    (1) - (2) //Original code calculation

=(1) + (-2)

=(0000 0001) + (1000 0010)

=(1000 0011)

=(-3) // wrong result

Because there is no problem in the addition of two positive numbers. So it was found that the problem occurred in the negative number with the sign bit.

-------------------------------------------------------------------------------------------------------------

Complement code: In order to solve the error in the subtraction operation of the original code, the concept of the inverse code is introduced. The inverse code is generated by inverting the remaining bits in the original code bit by bit except the sign bit. The value space of the inverse code is the same as the original code and corresponds one-to-one.

The one's complement operation rule is to perform the calculation column by column from the low order to the high order. The addition of 0 and 0 is 0, the addition of 0 and 1 is 1, and the addition of 1 and 1 is 0, but a carry 1 is generated and added to the next column.

If a carry is generated after the most significant bits are added, the final result should be added by 1, that is, (0000 0001).

With the inverse code operation, the result of the operation is also the inverse code. When converting to a true value, if the sign bit is 0, the digit remains unchanged; if the sign bit is 1, the result should be negated to obtain its true value.

If one's complement is used to calculate:

    (1) - (1) //Complement operation

=(1) + (-1)

= (0000 0001) + (1111 1110) // The original code of -1 is (1000 0001), and the inverse of each bit except the sign bit is (1111 1110)

=(1111 1111) //The sign bit is negative, and the result needs to be inverted.

=(1000 0000)

=(-0) //The result is wrong, 0 should be unsigned.


    (2) - (1) //Complement operation

=(2) + (-1)

= (0000 0010) + (1111 1110) + (0000 0001) // The original code of -1 is (1000 0001), and the bits except the sign bit are inverted to get (1111 1110), and the highest bit is added to generate a carry, then Add (0000 0001) to the final result.

= (0000 0001) //The sign bit is positive, and no inverse conversion is required.

=(1) //The result is correct

----------------------------------------------------------------------------------------------------------------------

Complement code: There are (+0) and (-0) problems in the inverse code operation. In people's calculation concept, zero is not divided into positive and negative, so the concept of complement code is introduced.

The complement of a negative number is to add 1 to the inverse code, while the positive number remains unchanged, and the complement of the original code of the positive number is the same.

If one's complement is used to calculate:

    (1) - (1)

=(1) + (-1)

= (0000 0001) + (1111 1110) + (0000 0001) //The complement of (1000 0001) is (1111 1110), and the complement is equal to the complement plus 1

=(0000 0000)

=(0) //The operation is correct

Therefore, the above problems were not solved until the introduction of the complement operation.

Guess you like

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