Talking about the complement of the original code in the computer

Original code

positive number

The original code in the positive number is relatively simple, directly convert the positive number into a binary number, the original code is the highest digit of 0 and the remaining digits are the binary number. When the number of digits is not enough, just add 0 directly forward. For example, how the original code of 42 is calculated, as shown in the figure below, the Conversion
final original code is:0101010If its 16-bit original code is required, how to directly add 0 to the front can be like 42 16-bit original code is0000000000101010

negative number

The original code in the negative number is similar to the positive number, but it is a bit more complicated. First, find the binary number of the absolute value of the negative number, and then write 1 in the highest bit and write the binary number of the negative absolute value in the other digits. For example, the absolute value of -42 in binary number 42 is101010, Then the original code of -42 is1101010If the number of digits is not enough, let the highest digit be 1, then add 0 in the middle and finally write the binary number of the absolute value of the number. For example, the 16-bit original code of -42 is:1000000000101010

Inverse code

The inverse code can be obtained by making a slight change on the basis of the original code.

positive number

The inverse code of a positive number is its original code. For example, the inverse of 42 is0101010When the number of digits is not enough, you can add 0 to the front. For example, the 16-bit complement of 42 is:0000000000101010

negative number

The inverse code of a negative number keeps the highest bit of its original code unchanged, and the rest of the bits are inverted. Negation means that 0 becomes 1 and 1 becomes 0.
For example, the original code of -42 is1101010Then its inverse code is:1010101, When the number of digits is not enough, add 1 to the front, such as the 16-bit complement of -42:1111111111010101

Complement

Numbers are stored in the computer as one's complement, and the one's complement is also used the most.

positive number

The complement of a positive number is its original code. For example, the inverse of 42 is0101010When the number of digits is not enough, you can add 0 to the front. For example, the 16-bit complement of 42 is:0000000000101010

negative number

The complement of a negative number is just +1 on the basis of its complement. For example, the complement of -42 is 1101010, and its complement is:1010110, When the number of digits is not enough, add 1 to the front. For example, the sixteen-digit complement of -42 is:1111111111010110

Tips

Complement

When I calculate the complement of a negative number, I do not first find the original code of the negative number, then the inverse code of the negative number, and then the complement of the negative number. We know that the original code of a positive number is equal to the inverse code and the complement code is better. So we can find the complement of the positive number first. For example, the complement of the original code of 42 is:0101010, If you require the inverse of -42, you can find the inverse of 42 of its absolute value, and then invert the number and +1 to get the inverse of -42:1010110

One's complement

There is nothing to say about the inverse of a positive number. The inverse of a negative number is to reverse the inverse of a positive number by bit. For example, the inverse of 42 is0101010, The opposite of -42 is1010101

Quickly find the number represented by the complement

For a negative number, you can first reverse its bits and then +1 to get the absolute value of the number it represents, and then add a sign to make this negative number.

Guess you like

Origin blog.csdn.net/weixin_47617598/article/details/114793670