Original code, inverse code, complement code operation

Due to the binary principle of computers, computers can only recognize binary numbers composed of 0 and 1. No matter what instructions are given to the computer, they will eventually be translated into a series of 0 or 1 combinations. Therefore, the operations between these binary numbers must also be performed through a series of rules and expressions. These operation expressions include original code, inverse code, and complement code. The following will be discussed in logical order.

1. Original code operation

At the very beginning, operations between binary numbers were required. The calculation rules designed were calculated using the original code. The representation of the original code is: the highest bit represents the sign of the number, that is, positive and negative, and the remaining bits represent the machine number (binary number) of the absolute value of the number, such as the following table:

decimal binary original code
1 1 00000001
2 10 00000010
3 11 00000011
-1 10000001
-2 10000010
-3 10000011

1+2 uses the original code to calculate the process: 00000001+00000010=00000011, the result is 3, correct.

But at this time, when it comes to the operation of negative numbers, the following problems will occur:

3-1 (subtracting a number is equivalent to adding its opposite number, that is, 3+(-1)) is calculated with the original code:

00000011+10000001=10000100, the result is -4, obviously wrong.

Therefore, errors will occur when using the original code to calculate operations involving negative numbers. In order to solve this problem, an inverse code expression is proposed.

2. Inverse code operation

Representation of one’s complement: the one’s complement of a positive number is itself, the highest bit of the one’s complement of a negative number remains unchanged, and the rest of the bits are reversed, as shown in the following table:

decimal binary original code inverse code
1 1 00000001 00000001
2 10 00000010 00000010
3 11 00000011 00000011
-1 10000001 11111110
-2 10000010 11111101
-3 10000011 11111100

The appearance and expression of one's complement solves operations involving negative numbers:

Because the inverse code of a positive number is the same as the original code, the operations between positive numbers and positive numbers are not affected, and correct operations can also be performed when negative numbers are involved:

2-3: 00000010+11111100=11111110, the original code of the number whose inverse code is 11111110 is 10000001, converted to decimal is -1, correct.

1-3: 00000001+11111100=11111101, the original code of the number whose inverse code is 11111101 is 10000010, converted to decimal is 2, correct.

However, in the following cases, the inverse code cannot be used for calculation:

2-1 Calculation process with inverse code: 00000010+11111110=(1) 00000000, the highest digit is overflowed and omitted, the original code of the number whose inverse code is 00000000 is 00000000, which is 0, an error.

3-1 Inverse code calculation process: 00000011+11111110=(1) 00000001, the highest bit overflow is omitted, the original code of the number whose inverse code is 00000001 is 00000001, which is 1, an error.

1-1 Calculation process with inverse code: 00000001+11111110=11111111, the original code of the number whose inverse code is 11111111 is 10000000, and the result is -0, because -0 is impossible, so the operation of inverse code is no longer applicable at this time, an error.

To avoid this, two's complement is proposed.

Three's complement operation

Representation of the complement: the complement of a positive number is itself, and the complement of a negative number is its inverse plus 1:

decimal binary original code inverse code Complement
1 1 00000001 00000001 00000001
2 10 00000010 00000010 00000010
3 11 00000011 00000011 00000011
-1 10000001 11111110 11111111
-2 10000010 11111101 11111110
-3 10000011 11111100 11111101

1+1 calculation process with complement code: 00000001+00000001=00000010, result 2, correct

1-2 Computation process with complement code: 00000001+11111110=11111111, the result is -1, correct.

Then go to calculate 2-1:

2-1 complement operation: 00000010+11111111=(1)00000001, the original code of the number whose complement is 00000001 (the highest bit overflows) is itself, that is, 00000001, which is 1 in decimal, correct.

3-1 Inverse code calculation process: 00000011+11111111=(1)00000010, the original code of the number whose complement code is 00000010 (highest digit overflow) is itself, that is, 00000010, which is 2 in decimal, which is correct.

1-1 Calculation process with complement code: 00000001+11111111=00000000, the rain of the number whose complement code is 00000000 (the highest digit overflows) is itself, that is, 00000000, which is 0 in decimal, which is correct.

Remark:

The result of using the original code is also the original code, the result of using the inverse code is also the inverse code, and the result of using the complement code is also the complement code. There is an adder in the computer, but there is no subtractor, so the subtraction is converted into addition. It is represented by an 8-bit binary number, which is determined by the word length of the computer. You can also use 16 bits to represent it, and the highest bit represents the sign bit. Computers use complement codes for calculations, and the data we input will be converted into corresponding complement codes by the computer itself.



 

Guess you like

Origin blog.csdn.net/m0_51660523/article/details/121998628