[Computer composition principle] Magical complement

There are two problems that have not been solved when learning complements

1. Why a series of bitwise inverted operations can be converted into the correct complement

2. Where is the convenience of using complement code than original code?

1. Get the complement by bit-wise inversion operation

For example, the decimal number 59 converted into an eight-digit binary number is 59-32 => 27-16 => 11-8 => 3-2 => 1

0011 1011   

If it is -59, the binary code is 1011 1011

The first convenience of using the inverse code than using the original code is that the value range can be increased by one. The 8-bit binary range is -127~127

The range of complement is -128~127

Our normal operation to obtain the complement is that the sign bit remains unchanged, the other valid bits are inverted bit by bit, and then the last bit is added by 1 to get,

Then the calculation definition of the complement is [complement]=M+[original] where the overflow bit is directly discarded. Where M=2^n

There are two cases here, the first is the case of positive numbers , such as the binary original code of 59 0011 1011

If M is added, the most significant bit of 1 0011 1011 overflows and rounds it down, so the complement of a positive number is the same as the original code.

Then according to the simple operation, reserve the sign bit 0, other bits are inverted 0100 0100 and the last bit plus one is 0100 0101. Obviously, positive numbers cannot be taken in this way, so these words above are nonsense....sorry

The second case is a negative number , for example, -59 and complement binary code is calculated

1011 1011

1100 0101

It can be seen that if the last digit does not add 1, and only the bitwise inversion operation is performed, the two numbers must add up to 1111 1111, then this number plus one is 1 0000 0000, which is M

Therefore, according to this calculation, -[original]+[complement]=M satisfies the definition of complement calculation, so this method can be used to calculate the complement of negative numbers. ok

2. Where the complement code is more convenient than the original code

The description in all related books is the original code. In the calculation, it is necessary to consider the symbol and the actual addition and subtraction operation and carry out the corresponding definition, but the complement is not the same, it is ok to add it directly

There are three situations here, one is positive + positive, the other is positive + negative, and the third is negative + negative

The first

Positive number + positive number is definitely as convenient as the original code, the sign bit is 0, then it is the same as ordinary addition.

The second

Positive + negative

If it is the original code, there are indeed many inconveniences. The absolute value and the final sign must be considered.

For example, if the calculation of 59-26 is the original code, the sign and absolute value of both parties should be considered, if it is the complement code calculation

-26 is 1001 1010 and the complement is 1110 0110 

59 is 0011 1011 

The two's complement is 1 0010 0001 and the highest bit overflow is 33 

The same goes for negative numbers + negative numbers

That is, the complement code turns the operation of adding a negative number into adding a positive number, and how does this positive number come from?

It is equivalent to a clock. If you need to reduce the pointer by two hours, you can also dial 10 hours to solve the problem. An 8-bit binary number is equivalent to a large dial with 128 graduations.

The one's complement is equal to the one's complement +1, the one's complement is subtracted from the original number from 127, and adding one is equivalent to subtracting the original number from 128. Adding this number to the subtracted number will just subtract the previous number.

Guess you like

Origin blog.csdn.net/mid_Faker/article/details/112132216