In simple terms, two's complement is a way to store negative numbers in computer memory. Whereas positive numbers are stored as a normal binary number.
Let's consider this example,
The computer uses the binary number system to represent any number.
x = 5;
This is represented as 0101.
x = -5;
When the computer encounters the - sign, it computes it'sits two's complement and stores it.
That is, 5 = 0101 and its two's complement is 1011.
The important rules the computer uses to process numbers are,
- If the first bit is
1then it must be a negative number. - If all the bits except first bit are
0then it is a positive number, because there is no-0in number system (1000 is not -0instead it is positive8). - If all the bits are
0then it is0. - Else it is a positive number.