Recently in the Brush C and the topic of the pointer, brush to a question need to complement the knowledge, in their own brain a find, found that the concept has been somewhat blurred. Just go to the podcast application and try the water!
We usually work together small partners always like to make a joke, I give you a handful of sand you build me a computer out! And indeed, the core component of the computer is the sand, is this the Taoist Sansheng of all things?
Oh, pull away ah.
On the other, the sand can be said to constitute the body of the computer, the idea of the computer, the essence of the soul is composed of what? Today's protagonist came: binary! In this article we don't say anything that Taixu is too big (in fact I can't say 233). Let's just say how the numbers are represented inside the computer by binary.
First, several concepts: original code, anti-code, complement
What is the original code?
The original code is an early way to represent numbers: a positive number, converted to bits is the original code for this positive. The absolute value of a negative number is converted to bits and then 1 is the original code of the negative number.
To illustrate:
The original code for type 3 of int is 11B (b for Bits) and four bytes on 32-bit machine, so the high 0 is:
00000000 00000000 00000000 00000011
The bits of the absolute value of type INT-3 is the above 11B expansion and the high 0 is:
10000000 00000000 00000000 00000011
But the original code has a few shortcomings, 0 points of two +0 and-0. It's weird, isn't it? Also, when the addition of different symbols or the subtraction of the same symbol, can not directly determine the positive and negative results. You need to compare the absolute value of two values, then add and subtract, and the last sign bit is determined by the absolute value. So the anti-code is produced.
What is anti-code?
The inverse of a positive number is the original code, negative inverse code equals the original code in addition to the sign bit
To illustrate:
The inverse of the type int 3 is
00000000 00000000 00000000 00000011
Like the original code, nothing to say.
The anti-code of type int 3 is
11111111 11111111 11111111 11111100
Except for the symbol bit, all bits are reversed.
Solve the problem of adding and subtraction operations, but still have a plus or minus 0 points, and then to the complement
What is the complement?
The complement of a positive number is the same as the original code, and the complement of the negative number is the inverse of all the bits in the original code except for the sign bit (get the inverse code), then the lowest bit plus 1.
Or an example to illustrate:
The 3 complement of the int type is:
00000000 00000000 00000000 00000011
The complement of type INT-3 is
11111111 11111111 1111111 11111101
is its anti-code plus 1
Finally, summarize:
The inverse code and complement of positive numbers are the same as the original code.
The inverse of a negative number is the original code of the numbers in addition to the sign bit, you take the inverse.
The complement of negative numbers is the original code of the number except the sign bit, and then the last one plus 1.
respective Advantages and disadvantages:
The original code is best understood, but the addition and subtraction is not convenient, there are two zeros.
Anti-code slightly more difficult to solve the problem of addition and subtraction, but there is a 0
The complement is difficult to understand, others have no shortcomings.
The relationship between the central code, the inverse code and the complement of the computer