Two's Compliment
Twos complement is a commonly used technique in binary coding to represent both positive and negative values. Luckily, its pretty easy to determine what the corresponding decimal value is by using the following equation:
With this equation in mind, one can see how the two tables from Wikipedia's page on Two's Complement were derived.
3-bits two's-complement integers | ||
---|---|---|
Bits | Unsigned Value | 2's Complement Value |
011¹ | 3 | 3 |
010 | 2 | 2 |
001 | 1 | 1 |
000 | 0 | 0 |
111 | 7 | -1 |
110 | 6 | -2 |
101² | 5 | -3 |
100 | 4 | -4 |
¹ Example calculation would work out to be –> 3 - (0)*(7+1) = 3
² Example calculation would work to be –> 5 - (1)*(7+1) = -3
8-bits two's-complement integers | ||
---|---|---|
Bits | Unsigned Value | 2's Complement Value |
0111 1111 | 127 | 127 |
0111 1110¹ | 126 | 126 |
0000 0010 | 2 | 2 |
0000 0001 | 1 | 1 |
0000 0000 | 0 | 0 |
1111 1111 | 255 | -1 |
1111 1110² | 254 | -2 |
1000 0010 | 130 | -126 |
1000 0010 | 129 | -127 |
1000 0010 | 128 | -128 |
¹ Example calculation would work out to be –> 126 - (0)*(255+1) = 126
² Example calculation would work out to be –> 254 - (1)*(255+1) = -2