The machine number is both the original code and the complement code. The signed binary numbers represented in computers are called machine numbers. Machine numbers include three representations: original code, one's complement and complement. The original code is a binary number with a sign bit added. The sign bit of a positive number is 0, the sign bit of a negative number is 1, and the sign bit is the highest bit; the complement of a positive number is its original code, and the complement of a negative number is the sign bit. The other bits are unchanged, and the other bits are inverted; the complement of a positive number is its original code, and the complement of a negative number is the complement of 1.
The operating environment of this tutorial: Windows 7 system, Dell G3 computer.
After understanding the machine number, we found that the machine number includes "source code", "reverse code", and "complement code" Representation form;
Because the machine number refers to the binary representation of a number in the computer. The first bit of the machine number is the sign bit. The sign bit of a positive number is "0" and the sign of a negative number. bit is "1".
For example: The number of machines for 5 is: 00000101
The number of machines for -5 is: 10000101 (taking 8-bit binary as an example)
True value refers to the real value corresponding to the machine number with sign bit.
For true values, they are generally expressed in decimal notation, but can also be expressed in binary. True values are rarely expressed in other bases.
For example: The true value of 10000101 = -0000101 = -5
The true value of 00000101 = 0000101 = 5
The original code is a binary number with a sign bit added. The sign bit of positive numbers is 0, the sign bit of negative numbers is 1, and the sign bit is the highest bit. My personal understanding is to convert " " in the true value to 0 and "-" to 1.
The representation of the original code is that the first bit is the sign bit plus the absolute value of the true value.
For example: -000101 (true value) = 10000101 (original code)
00000101 (true value) = 00000101 (original code)
Because the first bit is the sign bit , so the range of the original code in 8-bit binary is:
11111111~01111111 That is -127~127
The original code is the easiest representation for the brain to understand and calculate.
The complement of a positive number is itself; the complement of a positive number is its original code, and the complement of a negative number is the symbol The bit remains unchanged, and the other bits are inverted (0 becomes 1, and 1 becomes 0). The complement code of a negative number is that the sign bit remains unchanged, and the other bits are inverted.
The inverse code is based on the original code. If it is a positive number, it is the same as the original code. If it is a negative number, the first sign remains unchanged and the remaining values are inverted.
For example: 00000101 (original code) = 00000101 (reverse code)
10000101 (original code) = 11111010 (reverse code)
Reverse code is not easy for the brain to understand. It is usually converted into original code and then calculated.
The complement code is based on the original code. If it is a positive number, it is the same as the original code. If it is a negative number, except for the first symbol, The remaining values are inverted (one's complement code), and based on it, it is 1.
For example: 00000101 (original code) = 00000101 (reverse code) = 00000101 (complementary code)
10000101 (original code) = 11111010 (reverse code) = 10000011 (complementary code)
For negative numbers, the human brain cannot intuitively see the value in the two's complement representation, and usually it is necessary to convert it into the original code and then calculate the value.
It is easy to understand using only the original code, but for computers, it is necessary to distinguish the first Positive and negative consume a lot of resources, so people found ways to use sign bits to participate in operations.
We know that according to the algorithm, subtracting a positive number is equivalent to adding a negative number, that is: 1-1 = 1 (-1) = 0, so the machine can only add but not subtract, so The design of computer operations is even simpler.
Exploration Plan 1.0 (original code):
1 (-1) = 0
00000001 (original code) 10000001 (original code) = 10000010 (original code) = -2
Exploration of original code calculation plan failed
Additional investment!
Exploration plan version 2.0 is released (reverse code):
00000001 (original code) 10000001 (original code) = 00000001 (reverse code) 11111110 (reverse code) = 11111111 (reverse code) = 10000000 (original code) = -0
Update description: Solve the problem of the first symbol participating in the operation, which greatly reduces the resources consumed by the computer for binary operations, but "0" and "-0" appear Repeated situations can be improved!
Exploration plan version 3.0 is released (complementary code):
00000001 (original code) 10000001 (original code) = 00000001 (reverse code) 11111110 (reverse code) = 00000001 (complementary code) 11111111 (Complementary code) = 00000000 (Complementary code) = 00000000 (Original code) = 0
Append: (-1) (-127) = (-128)
10000001 (Original code) 11111111 (original code) = 11111110 (reverse code) 10000000 (reverse code) = 11111111 (complementary code) 10000001 (complementary code) = 10000000 (complementary code) = -128
Update description: Solve the problem of version 2.0 A meaningless binary "-0", the binary range is further expanded from version 1.0 (-127~127) to (-128~127). The same principle can also be used for other binary bits
For example: int has 4 bytes and 32 bits, and the range represented is (-2^31~2^31-1)
Since version 3.0 is more in line with the concept of environmental protection, it is the best choice for computers to store binary select.
For more related knowledge, please visit the FAQ column!
The above is the detailed content of Is the machine number the original code or the complement code?. For more information, please follow other related articles on the PHP Chinese website!