Complement and modulo, complement
I. Reasons for this article
I did some research on reverse code, complement code, and floating point number yesterday, but there are still some omissions. I discussed it with the fans in the dormitory at night. Instead, I raised a very interesting question, so with this article ~~ We don't know why there is a code complement. We only know why it is called a "complement" code.
Ii. Model
"Module" refers to the counting range of a metering system. Such as clock. A computer can also be seen as a metering machine. It also has a measurement model ".
For example:
The Metering range of the clock is 0 ~ 11, modulo = 12.
The n-bit computer metering range is 0 ~ 2 (n)-1, modulo = 2 (n ). [Note: n indicates the Index]
The "module" is actually the amount of "overflow" produced by the metering device.Its value cannot be expressed on the metering device, and the metering device can only represent the output mode
Remainder. Any metered tool with modulus can be reduced to addition.
For example, if the current hour hand points to 10 and the exact time is 6, there are two ways to adjust the time:
One is a four-hour switch, that is, 10-4 = 6.
The other is to distribute the traffic for 8 hours: 10 + 8 = 12 + 6 = 6
In a 12-module system,The effects of adding 8 and reducing 4 are the same.Therefore, any operation minus 4 can be replaced by 8.
For "modulo", 8 and 4 complement each other.In fact, in 12-mode systems, 11, and 6 all have this feature. The common feature is that the sum of the two equals the modulo.
This makes it clear that even the "modulo" here is related to the Modulo for Learning Division and the mod in the computer ~~
Iii. Code complementing Principle
I have just mentioned the concept of Modulo. Now let's look at the relationship between computer captioning and modulo.
Computers have the same concept and method. For n-bit computers, if n = 8, the maximum number can be 11111111.
Addition 1 is called 100000000 (9 bits), but because there are only 8 bits, the highest bits 1 is naturally lost. 00000000 again, so the 8-bit binary system
The modulo is 2 (8 ). In such a system, the subtraction problem can also be converted into an addition problem. You only need to use the corresponding population to represent the reduction.
.
The complement number is used to process the computer logarithm, that is, the complement code.
Two other concepts
One's complement refers to positive number = original code, negative number = reverse code.
Two's complement refers to the complement code that is usually referred.
Appendix:Algebraic interpretation of complement code
Any number a can be expressed:
-A = 2 ^ (n-1)-2 ^ (n-1)-;
If a is a positive number,-a is a negative number. Based on the binary to decimal number method, we can express a:
A = k0 * 2 ^ 0 + k1 * 2 ^ 1 + k2 * 2 ^ 2 + ...... + K (n-2) * 2 ^ (n-2)
Here k0, k1, k2, k (n-2) is 1 or 0, and here set the binary number of a to n bits, that is, the modulo is 2 ^ (n-1 ), 2 ^ (n-1) the second item is expanded as follows:
1 + 2 ^ 0 + 2 ^ 1 + 2 ^ 2 + ...... + 2 ^ N-2)
-a=2^(n-1)-2^(n-1)-a
,2^(n-1)-a
Substitution
A = k0 * 2 ^ 0 + k1 * 2 ^ 1 + k2 * 2 ^ 2 + ...... + K (n-2) * 2 ^ (n-2)
And
2 ^ (n-1) = 1 + 2 ^ 0 + 2 ^ 1 + 2 ^ 2 + ...... + 2 ^ N-2)
.
2 ^ (n-1)-a = (1-k (n-2) * 2 ^ (n-2) + (1-k (n-3) * 2 ^ (n-3) + ...... + (1-k2) * 2 ^ 2 + (1-k1) * 2 ^ 1 + (1-k0) * 2 ^ 0 + 1
This step demonstrates the algebraic principle of the rule of inverse addition 1.
Principle:
Because k0, k1, k2, k3 ...... 1-K0, 1-K1, and 1-K2 operations are bitwise in binary. Why do we add 1? It is 2 ^ (n-1) the second expansion has another reason. -A = 2 ^ (n-1)-2 ^ (n-1)-a, and-2 ^ (n-1) is not explained. This is the first 1 in the complement code, when the first 1 is converted to decimal, it must be multiplied by 2 ^ (n-1), which is the n-bit binary modulo.
Iv. complement and Model
After such a circle, I finally came back, and there was always a gathering time. Check the relationship between the complement code and the modulo.
Let's take a look at the derived formula:
2 ^ (n-1)-a = (1-k (n-2) * 2 ^ (n-2) + (1-k (n-3) * 2 ^ (n-3) + ...... + (1-k2) * 2 ^ 2 + (1-k1) * 2 ^ 1 + (1-k0) * 2 ^ 0 + 1
In fact, they all mean it,
Summary:
(1) 2 ^ (n-1) on the left is the binary "modulo", for example, 8-bit binary number, modulo 2 ^ 7 = 128
(2) a on the left is the source code of a number.
(3)AllYesA's Complement(Reverse + 1)
It is explained here: a number (negative)
Mode = source code + complement code
Example:
A [original] =-16D =-001 10000b = 1001 0000 [count]
A [inverse] = 1110 1111
A [Supplement] = 1111 0000
In turn, remove the symbol bit.
Modulo = a [original] + a [population], I .e.: 128D = 001 0000 + 111 0000
Because the complement in the computer is unique for a number, it can be used to represent a number.
Another problem is that-0 can be used to represent-128, because-0 and-128 complement each other.
-- END --
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.