The appearance of module and complement
Junior high school or high school or primary school let us know such a thing:
Turn left? 90 degrees, 270 degrees to the right, no matter how many turns, the final position is the same;
270+90=360;
When the minute hand is set 20 minutes backward and 40 minutes forward, the effect is the same regardless of the hour hand.
20+40=60;
If you subtract 25 from 87 and add 75, the effect will be the same regardless of the hundred digits.
25+75= 100;
……。
In short, there will be certain preconditions that will not be considered, which shows that the relationship between the two is complementary. The calculated values of 360, 60, 100 are modules in different situations. If you know the module, you can find the complement of a number.
By replacing the original number with complement, addition can be used instead of subtraction, and a carry is the value of a module.
Binary module:
How many bits are involved in the operation, and the modulus is how many zeros are added after 1.
For example, 2-bit binary numbers participate in the operation, and the modulus is100; That is 2 2 = 4.
8-bit binary numbers participate in the operation, and the modulus is10000000; That is 2 8 = 256.
Two's complement:
Find the complement of binary number, the purpose is to store it in the computer. Generally speaking, it is based on
Discuss the complement of 8-bit binary number.
Adding a positive number to the calculation is not a necessary condition for finding the complement; Only by subtracting (or adding negative numbers) do we need to complement the subtraction. Supplement is based on
It is defined by this requirement: positive numbers are constant, and negative numbers are norms minus absolute values.
For example, X =-126, and its complement is 10000 10. The calculation method is as follows:
1 0000 0000
— 0 1 1 1 1 1 10
—————————————
1000 00 10
In fact, it is to store data for simple calculation. Since complement can do this, why are the original code and the anti-code written in the book confused? Personally, I don't think it is very useful, and it doesn't exist in the computer. It just vividly explains how to find the complement of binary, which only plays the role of an intermediate calculation process, or it takes several rounds to write the complement of a number at once.