banner
指数爆炸

指数爆炸

Welcome!

What is two's complement?

What is the complement code?
The complement code is using positive numbers to represent negative numbers.

Example:

  1. For example, when representing -1 with an 8-bit binary number, we only need to find the complement code of 1 (00000001). We invert each digit of 00000001, changing 0 to 1 and 1 to 0, and then add 1 to the inverted result. Finally, it is converted to -1 (11111111).

  2. Another example is representing -5. The binary number for 5 is 0101. By inverting 0101 and adding 1, it is converted to 1011 (-5).

Actually, you don't really need to worry about the concepts taught by the teacher:
The original, inverse, and complement of positive numbers are the same, while for negative numbers, the original, inverse, and complement need to be calculated. Also, what is stored in the computer is the complement code, not the original code.

In fact, the computer stores these numbers in this way, just using the complement code to represent negative numbers.

Why do we use the complement code to represent negative numbers?
Because all decimal calculations in the computer are binary operations, and all subtractions in the computer are additions. If we don't use the complement code, then 1-1 would equal -2. For example:
0001-------------(1)
1001-------------(-1)
0001+1001=
1010-------------(-2)
So we need to use the complement code to represent negative numbers.
0001-------------(1)
1111-------------(-1)
0001+1111=
10000------0000-------(0)

Why is it called the complement code?
It is probably because when a number is added to its complement code, the result is 0. Actually, if you think about it, when representing -5, it is represented using 5, so -5 and 5 are opposite numbers.

Small tip:
Since a number and its complement code are opposite numbers, the complement code can be converted back to the original number by inverting and adding 1.
-5 (1011)------invert+1-------5 (0101)

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.