Here, binary computing can be divided into two types: addition and subtraction and multiplication and division. This article discusses addition and subtraction.
(1) prerequisites-data storage and representation on computers
Start with a question: the value range of byte (1 byte, 8 bits) in Java is-2 ^ 7 <= value range <= 2 ^ 7-1, or write-128 <= value range <= 127. Why?
Question: We generally think that if one of the eight digits is used as the symbol, it should be 11111111 <= value range <= 01111111, or-127 <= value range <= 127, -Where did 128 come from?
Starting from the original code, the original code is the binary representation of the value in the computer. For example, 5 is represented as 0000 0101 in binary format;
The second is the anti-code. As the name implies, it is the anti-code. For positive numbers, the anti-code is the same as the original code. For negative numbers, the anti-code is the same as the original code (except the symbol bit), for example (0011 0111) inverse = 0011 0111 (1101 0010) Inverse = 1010 1101;
Re-fill,In the computer, all values are represented and stored by the complement code., The positive complement is the same as the original code, negativeComplete the code as "its reverse code + 1", For example, (0101 1101) Fill = 0101 1101 1101 (0010 1010) Fill = 1110
We will further explain the following code:
(1) Reduce to plus:
Because the CPU in the calculation only has the adder and there is no subtraction, the original code in the computer for subtraction will have the problem: for 1-1 = 0
If the value is 1 + (-1) = 0, the binary value 0001 + 1001 = 1001 is converted into a decimal negative 1 instead of 0. The occurrence of the complement operation solves this problem. Because the complement operation is used, the complement addition becomes: [x + y] fill = [x] fill + [y] fill at the same time, the subtraction of the complement becomes: [X-Y] fill = [x] fill-[y] fill = [x] fill + [-y] fill; here, we take subtraction as an example to describe the advantages of complement code. The preceding example is as follows: (0001) Complement + (1001) Complement = 0001 + 1111 = 0000 (the carry of the highest bit is omitted ), in this way, we get 0.
(2) are both 10000000 and 00000000 zero? -- About the origin of-128
The complement also solves the problem of two zeros in the original code (that is, + 0 and-0). Taking the octal format as an example, the value range of byte in Java should be-127 ~ -0 and + 0 ~ 127 indicates the existence of-0 and + 0, but after the two 0 values are converted to the complement code, the values are 10000000 and respectively, and the value is 0 at,We artificially set 10000000 to-128, Which is the origin of-128 in the above value range. Here, we will emphasize that both 10000000 and 00000000 are complementary codes, rather than the original codes.
(3) At the beginning, we came up with the "logical" question. What is the problem?
In fact, the problem is that we have been using the original code to see the problem, rather than making up the code. Here is an example I mentioned on Weibo to illustrate what a complement code is:
"Example of Clock: time difference between 0 and 6 (10-6) = 4 (clockwise), but you can also write (10 + 6) -12 = 4 (counterclockwise). 6 In the next sub-statement is the "Supplemental code", 12 indicates the "fixed length", and 12 indicates the "overflow ". "Fixed Length" and "overflow" are also the key to "Reducing to plus. This is a typical example of how to look at a low-dimensional space from a high-dimensional space and become more concise ."
Let's use an actual example to illustrate how to use "high-dimensional view of low-dimensional ":
In the example of clock analogy, We will slightly change the position of the complement code in the table, write it on the one-dimensional Digital axis, and then link the number axis to form a new "clock disc ".
Let's try an example: 3-1 = (011-001) original code operation = (011 + 111) Complement operation = 010 = 2
In other words, we can write 3-1 = 2 (clockwise operation), or 3 + 7-8 = 2 (counterclockwise operation)
This is an interpretation method that uses the hyperspace-Time Theory to look at the status from a high dimension.
(2) addition and subtraction
(1) conversion from the source code to the complement Code: The symbol bit must be extracted separately, and does not participate in "inverse addition ".For example:
(10000000) original converted to complement:
A. After the 0000000 value is reversed and the value is equal to 10000000
B. Because a gets more than seven digits, remove 1 of 10000000 in A and fail to give 1 to the question. It is emphasized that 1 in the question is different from 1 in.
(2) The complement operation is performed together with the symbol bit. Here, by the way, the operator number of binary multiplication is the same as that of decimal multiplication, And the symbol bit is calculated separately.
Binary computing (why does a computer use a complement code to store data)