Machine number, truth, source code, inverse and complement

All binaries in this paper are represented by 1 byte for convenience

I. Number of machines, truth value

1. The machine number

Machine number is the binary representation of a number in the computer. The highest bit of machine number in the computer is the sign bit. The sign bit of positive number is 0, and the sign bit of negative number is 1. Machine number includes source code, inverse code and complement code, which will be explained in detail later.

Such as:

The +2 in decimal has a word length of 8 in the computer, which is converted to 0000 0010 in binaryCopy the code

In the example above, 0000 0010 and 1000 0010 are the machine numbers

2. True value of the number of machines

Truth value is the real value corresponding to the number of machines with signed bits. Personally, it is used to represent the number of machines with signs instead of sign bits. For example:

The truth value for the number of machines, 0000, 0000 10, is +000, 0000 10, which is +2Copy the code

Two. Three representations of machine number: original code, inverse code, complement code

1. The original code

The source code is a binary number with one sign bit added. The positive sign bit is 0, the negative sign bit is 1, and the sign bit is the highest bit. Convert the “+” in the truth value to 0 and the “-” in the truth value to 1.

The decimal system The true value The original code
+ 2 + 000 0010 0000, 0010,
2 – 0010-000 1000, 0010,

It can be concluded that the size range of 8-bit binary numbers is [1111 1111, 0111 1111], that is, [-127, 127] corresponds to the size range of Java byte data types

2. Radix-minus-one complement

The inverse of a positive number is its original code, and the inverse of a negative number is the same sign bit, and the other bits are reversed (0 becomes 1,1 becomes 0).

The decimal system The original code Radix-minus-one complement
+ 2 0000, 0010, 0000, 0010,
2 – 1000, 0010, 1111, 1101,

3. The complement

The complement of a positive number is its original code, and the complement of a negative number is the inverse +1

The decimal system The original code Radix-minus-one complement complement
+ 2 0000, 0010, 0000, 0010, 0000, 0010,
2 – 1000, 0010, 1111, 1101, 1111, 1110,

4. Why use inverse and complement codes

When using the source code for calculation, it is easy for people to identify the symbol bits, and then directly calculate the other bits. However, for the computer design to identify the symbol bit is a very complex project, so the design of the time to consider the symbol bit directly involved in the calculation, so that the design of the computer is very simple. For addition, the sign bit does not affect the calculation. For subtraction, it is considered to convert to addition by adding negative numbers. If the subtraction calculation is performed directly by the source code:

3 -2 = 3 + (-2) = 0000 0011 + 1000 0010 = 1000 0101 = -5Copy the code

The result is obvious, if through the original code to directly let the symbol bit involved in the operation is not correct, so in order to solve the problem of subtraction, the concept of inverse code is introduced. If subtraction is performed by inverse code:

3 -2 = 3 + (-2) = 0000 0011 + 1000 0010(-2) = 0000 0011(-2) + 1111 1101(-2) = 1 0000 0000(-2) Result +1 = 0000 0001(inverse) = 0000 0001(original) = 1Copy the code

The result is correct. From the above example, it seems that there is no problem if the subtraction operation is performed by inverse code. Then why do we need complement?

2 -2 = 2 + (-2) = 0000 0010(original) + 1000 0010(original) = 0000 0010(negative) + 1111 1101(negative) = 1111 1111(negative) = 1000 0000(original) = -0 0 + 0 = 0 0000 0000 + 0000 = 0000 0000 + 0000 = 0000 0000 = 0Copy the code

As for the number 0, the positive and negative signs have no meaning, but after calculation, two different source codes [0000 0000] and [1000 0000] may represent the same number 0, which is obviously unreasonable, so the concept of complement is introduced at this time. If the above example is calculated by complement:

2 -2 = 2 + (-2) = 0000 0010 + 1000 0010 = 0000 0010 + 1111 1101 = 0000 0010 + 1111 1110 = 1 0000 0000(complement) -- the highest bit produces a carry, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 = 0000 0000 0000 0000 0000 000Copy the code

It can be seen from the above examples that complement perfectly solves the symbol problem of 0 and the problem that 0 has two different source codes. And [10000 0000] can also be used to represent -128:

-1-127 = -1 + (-127) = 1000 0001 + 1111 1111 = 1111 1110 + 1000 0000 = 1111 1111 + 1000 0001 = 1 1000 0000(complement) -- highest bit generates carry, carry discard = 1000 0000(complement)Copy the code

The result of -1-127 is -128. In the example above, the complement of -1 and -127 is also -128. But the 1000, 0000 actually corresponds to the previous -0, so there is no inverse and no source. To sum up, it can be seen that the use of complement code can not only solve the symbol problem of 0 and multiple source code problems, but also represent a minimum number. So for 1 byte, the range of source and inverse is [-127, 127], and the range of complement is [-128, 127], which can also be interpreted in Java int is [-2, 2-1].