After making the transistor, the mankind made the nand gate by using the transistor, and then made the adder by using the Nand gate. The adder solves the problem of addition. Only adder is not enough, but also need to solve the problem of subtraction calculation, but compared with adder, the design of subtraction hardware is more complex, increased the calculation time, can use adder to achieve the function of subtraction? This implementation uses the complement.

Why do computers use complement? Complement code can simplify the complexity of computer hardware circuit design.

For the signed number, the memory to distinguish between the symbol bit and the numeric bit, if the symbol bit and the numeric bit can be equal, let them participate in the operation, no longer to distinguish, only the adder can realize addition and subtraction operations at the same time, so that the hardware circuit becomes simple.

8 minus 3 is the same thing as 8 plus minus 3, 12 minus minus 9 is the same thing as 10 plus 9.

The cost of simplifying hardware circuits is that signed numbers are converted as they are stored and read. This conversion process involves the familiar source code, inverse code, complement code.

The original code

To convert an integer into binary form is its source code. For example, short a = 5; , the source code of a is 0000 0000 0101; Change a to a = -19; In this case, the source code of A is 1000 0000 0001 0011.

In popular understanding, source code is the original binary form of an integer.

Radix-minus-one complement

The inverse of positive and negative numbers is different.

For a positive number, its inverse is its source code (the source code is the same as the inverse code); The inverse of a negative number is the reversal of all the bits (numeric bits) in the source code except the sign bits, that is, 0 becomes 1, and 1 becomes 0. For example, short a = 5; , both the original and inverse codes of a are 0000 0000 0000 0101; Change a to a = -19; , the inverse code of A is 1111 1111 1110 1100.

Why need inverse code, the role of inverse code is equivalent to the negative number in mathematics, with a negative number, can realize the subtraction and addition operation unified into addition operation.

complement

Why do you need a complement when you have an inverse

Because of the existence of the special number 0.

To treat subtraction as addition, negative numbers need to be represented by inverse code, so the range of positive numbers represented by 8-bit binary inverse code is: +0 — +127; Negative numbers range from -127 to -0. However, two special encodings appear:

[0_0000000]=+0

[1_1111111]=-0

Plus 0 and minus 0 are both 0’s. In this way, the number “0” in the computer code is not unique. This is a no-no for computers, because there can be only one code for any number.

We know that 0 is neither positive nor negative. To solve the problem that the code is not unique, we use 0 as a positive number, i.e. +0, so that the code of 0 becomes: 0_0000000. The range of positive numbers represented by the 8-bit binary is still: +0 — +127. Negative numbers as a whole “move back one bit”, the inverse +1, {1_1111111} code no longer means -0, but -1. By extension, the minimum code {1_0000000} is -128, and the negative 8-bit binary representation ranges from -127 — -0 to -128 — -1, which successfully solves the problem.

This operation seems to be a “patch” on the inverse code, so it is called the complement code, the definition of the complement code is as follows:

1. The complement of positive numbers remains unchanged: 5 = 0_000 0000 0000 0101

-19 = 1_111 1111 1110 1100 + 1 = 1_111 1111 1110 1101

Calculation process of 5-19:

0_000 0000 0000 0101 + 1_111 1111 1110 1101 = 1_111 1111 1111 0010;

Converting the complement to the original is also easy: subtract 1 and invert the numeric bit.

1_111 1111 1111 0010:1000 0000 0000 1110 = -14

The problem that number 0 is not unique code in computer is solved successfully by using complement code, and subtraction to addition is also realized.

conclusion

Complement code is to solve the representation of negative numbers in the computer, and finally to solve the problem of computer subtraction operation. The fundamental reason for the use of complement codes in computers is, “Easy to design hardware!”

  • Don’t waste the number of codes;

  • Save computer judgement sign bit or judgement +/- operation trouble.

  • With complement, for addition and subtraction, there’s only one adder on the hardware;

  • With the addition and subtraction operation, with the program can achieve multiplication and division operation, without additional increase in hardware;

  • With addition, subtraction, multiplication and division, “all” arithmetic operations can be performed programmatically without additional hardware.