Relationship between original codes, reverse codes, and supplementary codes in the computer, and source code Complement
Recently, I have been covering questions about C and pointers, and I have learned how to fill in a question. I have searched my mind and found that the concept is vague. Just now that the application for the podcast has passed, let's try the water!
When we work together, our friends always like to joke. I'll give you a piece of sand, and you'll make me a computer! Think about it. Indeed, the core component of the computer, that is, the sand, has changed. Is this the thought of the three generations of everything in Taoism?
Oh, it's far away.
As the saying goes, the sand can be said to make up the computer's body. What is the essence of the computer's mind and soul? Today's leading character: Binary! In this article, we will not talk about things that are too high (in fact, I cannot say anything about 233 ). Let's simply talk about how numbers are represented in binary in a computer.
First, I will introduce several concepts: original code, reverse code, and complement code.
What is the original code?
The original code is a method used to represent a number in the Early Days: a positive number is the original code of this positive number. The absolute value of a negative number is converted into a binary number, and then 1 is the original code of this negative number.
Example:
The original code of int Type 3 is 11B (B indicates the binary bit), which occupies four bytes on 32-bit machines. Therefore, the high-position zero-padding is required:
00000000 00000000 00000000 00000011
The binary bit of the absolute value of-3 of the int type is the upper position after the above 11B is expanded:
10000000 00000000 00000000 00000011
However, the original code has several disadvantages: Zero + 0 and-0. That's strange, right! In addition, when performing addition operations on different symbols or subtraction operations on the same symbol, the positive and negative results cannot be directly determined. You need to compare the absolute values of two values, and then perform addition and subtraction. The last sign bit is determined by the absolute value. The reverse code is generated.
What is anti-code?
Positive anticode is the original code, and negative anticode is equal to all digits except the symbol bit.
Example:
The anticode of int Type 3 is
00000000 00000000 00000000 00000011
There is nothing to say like the original code.
The anticode of-3 of int type is
11111111 11111111 11111111 11111100
Remove all bits from the signed bits
It solves the problem of addition and subtraction, but there are still positive and negative zeros, and then the code is completed.
What is a supplemental code?
The positive complement is the same as the original one. The negative complement is the reverse of all the bits except the symbol bit. Then, the bitwise is added with 1.
Examples:
The complement of int Type 3 is:
00000000 00000000 00000000 00000011
The complement of-3 of int type is
11111111 11111111 1111111 11111101
It is the anti-code plus 1.
Summary:
The reverse and complement codes of positive numbers are the same as the original codes.
The negative number is the inverse of the original number except the symbol bit.
The complement code of the negative number is the inverse of the original code of the number except the symbol bit. Then, add 1 to the last digit.
Advantages and disadvantages:
The original code is better understood, but addition and subtraction are not convenient enough. There are still two zeros ..
The back code is a little more difficult, solving the problem of addition and subtraction, but there is still a zero
It is difficult to understand the code, so there are no other shortcomings.