Home > Common Problem > Is the machine number the original code or the complement code?

Is the machine number the original code or the complement code?

青灯夜游
Release: 2022-11-18 14:22:46
Original
16795 people have browsed it

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.

Is the machine number the original code or the complement code?

The operating environment of this tutorial: Windows 7 system, Dell G3 computer.

1. Machine number


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)

And "source code", "reverse Code" and "complement code" have the same representation form, and their relationship will be introduced in detail next.

2.True value


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

3. Original code

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.

4. Inverse code

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.

5. Complement code

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.

6. The process of using original code, inverse code and complement code


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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template