A common challenge when handling binaries is how to represent signed numbers in a way that is both machine- and human-friendly.
Traditional Approaches
One traditional approach to represent signed integers is the sign-magnitude method. Numbers such as +6 and -6 can be represented in 4 bits as:
0110 # +6 (0 for positive)
1110 # -6 (1 for negative)
The most significant bit is reserved for the sign, while the remaining bits represent the magnitude. However, there are a few notable drawbacks with this approach:
- There are two representations of zero. For example, in a 4-bit system, zero can be represented as either 0000 (+0) or 1000 (−0).
- The two representations of zero reflect another issue with the sign-magnitude approach—it makes arithmetic calculations complex. First, computers require extra logic to ensure that the different representations of zero are treated as equal. More importantly, since the binary representation of a number does not align with its magnitude, arithmetic comparisons and calculations cannot be performed in a regular manner—you would get incorrect results unless special logic is applied. For example, adding +5 and -5 using sign-magnitude in a 4-bit system results in:
0101
+ 1101
-------
10010 # Incorrect because it is not zero
Another approach is the one’s complement notation. Complement essentially means the number required to reach a fixed total. For example, the complement of 9 to reach 10 is 1. “One” here refers to the fact that the fixed total is 2n − 1, which in binary is a string of all 1s.
With this method, the negative counterpart of a positive number is produced by flipping all the bits of the positive number. Take +6 and -6 for example:
0110 # +6
1001 # -6
However, the one’s complement method does not solve the issues with sign-magnitude. It still results in two representations of zero, and arithmetic operations remain awkward.
Two’s Complement Notation
The two’s complement notation has many advantages over traditional approaches, making it the standard way to represent signed integers in modern computing.
Rather than complementing a fixed value of all 1s, two’s complement represents the negative of a number as its complement with respect to 2n. Specifically, in an n-bit system, the total range spans −2n-1 to 2n-1 − 1.
How It Works
Two’s complement is a fixed-width representation, meaning it can only represent a limited range of integers. For instance, in 4-bit two’s complement, the representable range is from −8 to +7.
Binary | Decimal |
---|---|
0000 | 0 |
0001 | 1 |
0010 | 2 |
0011 | 3 |
0100 | 4 |
0101 | 5 |
0110 | 6 |
0111 | 7 |
1000 | -8 |
1001 | -7 |
1010 | -6 |
1011 | -5 |
1100 | -4 |
1101 | -3 |
1110 | -2 |
1111 | -1 |
Positive integers are represented in normal binary form, while negative integers are represented using the following steps:
- Find the binary representation of the number’s absolute value (its positive counterpart).
- Flip each bit (i.e., 0s become 1s, and 1s become 0s).
- Add 1 to the result.
Take −3 as an example in a 4-bit system:
# Step 1: Binary of +3
0011
# Step 2: Flip bits
1100
# Step 3: Add 1
1101
So, −3 is represented as 1101.
Why Adding 1?
This is the key step that makes the complement relative to a power of 2. In the previous example, without the +1, −3 would be 1100, and adding +3 would result in 1111, which is −1, not 0. If we treated 1111 as 0, we would end up with two representations of zero again—just like one’s complement.
By adding 1 to the flipped bits, we ensure that adding a number and its two’s complement always yields zero, with only one unique representation of zero. For example:
0101 # +5
+ 1011 # −5
-------
10000 # The leftmost carry is discarded—result is 0000
This example also highlights a big advantage of two’s complement: subtractions can be performed as additions of negative numbers, which simplifies arithmetic logic and makes hardware more efficient.