Two's Complement
Two's Complement
Two's Complement
What is two's complement?
In IGCSE Computer Science, two's complement is a method of using signed binary values to represent negative numbers
Using two's complement the left most bit is designated the most significant bit (MSB)
In two’s complement, if the MSB is 1, it represents a negative number
In 8-bit, the MSB column has a weight of -128, while the remaining columns keep their positive values
-128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | |
|---|---|---|---|---|---|---|---|---|
1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | = -1 |
In the example above, to find the value, add together the column values where there is a 1
Here, all columns are 1, so the sum is -128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = -1
The two's complement representation of -1 is 11111111
Quick two's complement conversion
To represent -76
Write out the positive version of the number
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | |
|---|---|---|---|---|---|---|---|---|
0 | 1 | 0 | 0 | 1 | 1 | 0 | 0 | = 76 |
Starting from the least significant bit (right most column), copy out the binary values up to and including the first 1
-128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
|---|---|---|---|---|---|---|---|
1 | 0 | 0 |
For the remaining digits, invert them (0s to 1s/1s to 0s)
-128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
|---|---|---|---|---|---|---|---|
1 | 0 | 1 | 1 | 0 | 1 | 0 | 0 |
-128 + 32 + 16 + 4 = -76
The two's complement representation of -76 is 10110100