Original code, inverse code and complement code

Time:2021-5-12

1、 Concept explanation

Number of machines:

The binary representation of a number in a computer is called the machine number of the number. Machine number is signed, in the computer with a number of the highest place to store symbols, 0 represents a positive number, 1 represents a negative number.

For example, + 2 binary number: 0 (sign bit) 000 0010- Binary number: 1 (sign bit) 000 0010.

Among them, 0000 0010 and 1000 0010 are the number of machines.

True value:

Because the first bit is the sign bit, the formal value of machine number is not equal to the real value.

For example, the top 1000 100, where the highest 1 represents a negative number, and its real value is – 2 instead of 242 (1000 100 converted to decimal 242).

Therefore, for the sake of distinction, the real value corresponding to the signed machine number is called the true value of the machine number.

For example: 0000 0010 = + 000 0010 = + 2; 1000 0010 = -000 0010 = -2。

2、 Specific content

Original code:

The original code is the absolute value of the sign bit plus the true value.

+2 = 0000 0010 [original]
-2 = 1000 100 [original]

Value range.

[1111 1111, 0111 1111]   ==>   [-127, 127]

Reverse code:

Inverse code means that the inverse code of a positive number is the original code itself, and the inverse code of a negative number is based on the original code, with the sign bit unchanged and the rest bits reversed.

+2 = 0000 0010 [original] = 0000 0010 [reverse] - 2 = 1000 0010 [original] = 1111 1101 [reverse]

Value range.

[1111 1111, 0111 1111]   ==>   [-127, 127]

Complement:

The complement is that the complement of a positive number is the original code itself, while the complement of a negative number is based on the inverse code, + 1.

+2 = 0000 0010 [original] = 0000 0010 [reverse] = 0000 0010 [supplement] - 2 = 1000 0010 [original] = 1111 1101 [reverse] = 1111 1110 [supplement]

Value range.

[10 million, 0111, 1111] = > [- 128, 127] (- 128 only has complement, no original code and inverse code)

Why are Inverses and complements used?

//Original code.
1 + (- 1) = 0000 0001 [original] + 1000 0001 [original] = 1000 0010 [original] = - 2 (the result is wrong, so the inverse code appears.)
//Reverse code.
1 + (- 1) = 0000 0001 [inverse] + 1111 1110 [inverse] = 1111 1111 [inverse] = 1000 0000 [original] = - 0 (the result is correct, existing + 0 [0000 0000] [original], - 0 [100000 0000] [original],
But there is no difference between positive and negative, and there can only be one 0, so there is a complement.)
//Complement.
1 + (- 1) = 0000 0001 [complement] + 1111 1111 [complement] = 0000 0000 [complement] = 0000 0000 [original] = 0 (use 0000 0000 to represent 0, use 1000 000 to represent - 128.)

(- 1) + (- 127) = 1000 0001 [original] + 1111 1111 [original] = 1111 1110 [inverse] + 1000 0000 [inverse] = 1111 1111 [complement] + 1000 0001 [complement] = 1000 0000 [complement] = - 128 (- 128 only complement, no original code or inverse code.)

Using complement code, not only fixed the sign of 0 and two coding problems, but also expressed a minimum number (- 128).

 

The above is my understanding of the original code, inverse code and complement code. I hope it can help my friends in need.