![]() |
VOOZH | about |
There are three common ways to represent signed integers: sign-bit representation, 1’s complement, and 2’s complement. Since data is stored in bits, we need a method to store both positive and negative values in memory. To understand this, we start with a simple approach and then gradually improve it to arrive at a more efficient and practical solution.
In this method, the leftmost bit is used to show the sign of the number. 0 means positive and 1 means negative. The remaining bits store the value.
For example, in a 4-bit system, 1 bit is for sign and 3 bits store the number.
In an 8-bit system, 1 bit is for sign and 7 bits store the number.
| Sr. No. | Binary Representation | Decimal Value |
|---|---|---|
| 1 | 0000 | +0 |
| 2 | 0001 | +1 |
| 3 | 0010 | +2 |
| 4 | 0011 | +3 |
| 5 | 0100 | +4 |
| 6 | 0101 | +5 |
| 7 | 0110 | +6 |
| 8 | 0111 | +7 |
| 9 | 1000 | -7 |
| 10 | 1001 | -6 |
| 11 | 1010 | -5 |
| 12 | 1011 | -4 |
| 13 | 1100 | -3 |
| 14 | 1101 | -2 |
| 15 | 1110 | -1 |
| 16 | 1111 | -0 |
By using this approach, we are successfully able to represent signed integer. But when we analysis it more closely, we could observe following drawbacks:
In sign-bit representation, the leftmost bit shows the sign and the remaining bits store the value. Because of this, zero can be represented in two ways:
Both represent the same value (zero), but they have different bit patterns.
Why this is a problem:
As data size increases, we need to extend the number of bits (for example, from 32-bit to 64-bit). A good system should allow extension without changing the value.
| Decimal | 4-bit | 5-bit | 6-bit |
|---|---|---|---|
| +2 | 0010 | 00010 | 000010 |
| +7 | 0111 | 00111 | 000111 |
| -2 | 1010 | 10010 (! = 11010) | 100010 (! = 111010) |
| -7 | 1111 | 10111 (! = 11111) | 100111 (! = 111111) |
Let's try to add two binary numbers:
| Binary | Decimal | Binary | Decimal | Binary | Decimal | |||
|---|---|---|---|---|---|---|---|---|
| Number-1 | 0010 | +2 | 0111 | +7 | 1101 | -5 | ||
| Number-2 | 1010 | -2 | 1010 | -2 | 0011 | +3 | ||
| Binary addition | 1100 | -4 | 0001 | +1 | 0000 | +0 | ||
| Decimal addition | +0 | +5 | -2 |
Binary addition does not work correctly in this representation because the sign bit is treated separately from the value bits. When we perform normal binary addition, the result becomes incorrect since the sign bit is not handled as part of the actual number. This makes arithmetic operations complex and unreliable.
In this method, negative numbers are arranged in reverse order compared to the signed-bit method. This improves how negative values are handled.
| Binary | 1’s Complement Value | Signed Bit Value |
|---|---|---|
| 0000 | +0 | +0 |
| 0001 | +1 | +1 |
| 0010 | +2 | +2 |
| 0011 | +3 | +3 |
| 0100 | +4 | +4 |
| 0101 | +5 | +5 |
| 0110 | +6 | +6 |
| 0111 | +7 | +7 |
| 1000 | -7 | -0 |
| 1001 | -6 | -1 |
| 1010 | -5 | -2 |
| 1011 | -4 | -3 |
| 1100 | -3 | -4 |
| 1101 | -2 | -5 |
| 1110 | -1 | -6 |
| 1111 | -0 | -7 |
Example:
+2 : 0010
-2 : 1101
This method still has two zeros:
+0 : 0000
-0 : 1111
This creates confusion and wastes one value.
Extending bits keeps the value correct:
| Decimal | 4-bit | 5-bit | 6-bit |
| +2 | 0010 | 00010 | 000010 |
| +7 | 0111 | 00111 | 000111 |
| -2 | 1101 | 11101 | 111101 |
| -7 | 1000 | 11000 | 111000 |
Normal binary addition gives almost correct results, but one extra step is needed.
| Binary | Decimal | Binary | Decimal | Binary | Decimal | |||
|---|---|---|---|---|---|---|---|---|
| Number-1 | 0010 | +2 | 0111 | +7 | 1010 | -5 | ||
| Number-2 | 1101 | -2 | 1101 | -2 | 0011 | +3 | ||
| Binary addition | 1111 | -0 | 0100 | +4 | 1101 | -2 | ||
| Decimal addition | +0 | +5 | -2 |
If there is a carry from the leftmost bit, add it back to the rightmost bit.
| Binary | Decimal | Binary | Decimal | Binary | Decimal | |||
|---|---|---|---|---|---|---|---|---|
| Number-1 | 0111 | +7 | 1110 | -1 | 0111 | +7 | ||
| Number-2 | 1101 | -2 | 1001 | -6 | 1011 | -4 | ||
| Binary addition | (1) 0100 | +4 | (1) 0111 | +7 | (1) 0010 | +2 | ||
| Adding carry forward back | 0101 | +5 | 1000 | -7 | 0011 | +3 |
2’s complement is obtained by removing -0 from the 1’s complement table and shifting negative values one step down.
| Binary | 2’s Complement | 1’s Complement | Sign-Magnitude |
|---|---|---|---|
| 0000 | +0 | +0 | +0 |
| 0001 | +1 | +1 | +1 |
| 0010 | +2 | +2 | +2 |
| 0011 | +3 | +3 | +3 |
| 0100 | +4 | +4 | +4 |
| 0101 | +5 | +5 | +5 |
| 0110 | +6 | +6 | +6 |
| 0111 | +7 | +7 | +7 |
| 1000 | -8 | -7 | -0 |
| 1001 | -7 | -6 | -1 |
| 1010 | -6 | -5 | -2 |
| 1011 | -5 | -4 | -3 |
| 1100 | -4 | -3 | -4 |
| 1101 | -3 | -2 | -5 |
| 1110 | -2 | -1 | -6 |
| 1111 | -1 | -0 | -7 |
How to get binary representation of an integer in 2’s complement method?
Now we have only one representation of zero and it allows us to store total 16 unique values (+0 to +7 and -1 to -8).
Signed extension works perfectly for negative numbers.
| Decimal | 4-bit | 5-bit | 6-bit |
| +2 | 0010 | 00010 | 000010 |
| +7 | 0111 | 00111 | 000111 |
| -2 | 1110 | 11110 | 111110 |
| -7 | 1001 | 11001 | 111001 |
| Number-1 | Number-2 | Result |
|---|---|---|
| 0010 (+2) | 1110 (-2) | 0000 (0) |
| 0111 (+7) | 1110 (-2) | 0101 (+5) |
| 1011 (-5) | 0011 (+3) | 1110 (-2) |
| 1111 (-1) | 1010 (-6) | 1001 (-7) |
Overflow occurs when:
| Binary | Decimal | Binary | Decimal | Binary | Decimal | Binary | Decimal | |
| Number-1 | 1011 | -5 | 0010 | 2 | 0111 | +7 | 1011 | -5 |
| Number-2 | 1100 | -4 | 0110 | 6 | 1110 | -2 | 0011 | 3 |
| Addition | (1) 0111 | (0)1000 | (1)0101 | (0)1110 | ||||
| carry in to sign bit | 0 | overflow | 1 | overflow | 1 | no | 0 | no |
| carry out to sign bit | 1 | 0 | 1 | 0 |