Computers store everything as binary bits (0s and 1s). But how do we represent negative numbers in hardware? In this lesson, we break down unsigned integers and compare the three main signed number schemes: Sign-Magnitude, One's Complement, and Two's Complement.
Every memory cell or register consists of $N$ physical bits. Depending on how the processor interprets these bits, we have two types of numbers:
0 to 2N - 1.11112 = 1510.Historically, computer engineers explored three different ways to encode negative numbers in binary:
The Most Significant Bit (MSB) is the Sign Bit (0 = Positive, 1 = Negative). The remaining bits state the positive magnitude of the number.
+3 in 3 bits = 011 (Sign 0, Mag 11 = 3)-3 in 3 bits = 111 (Sign 1, Mag 11 = 3)+0 = 000 and -0 = 100). Requires separate hardware logic for addition and subtraction.Positive numbers are represented normally. To get the negative version of a number, simply invert all bits (NOT operation: swap 0s to 1s and 1s to 0s).
+3 in 3 bits = 011-3 in 3 bits = 100 (inverted 011)+0 = 000 and -0 = 111). Addition requires an extra "end-around carry" step.Positive numbers are represented normally. To get the negative version of a number: take the One's Complement and add 1.
Two's Complement = Invert Bits + 1
+3 in 3 bits = 011-3 in 3 bits: 100 (1's comp) + 1 = 101000). Addition and subtraction use the exact same adder circuit in the ALU!Below is the complete 3-bit comparison table showing how values from +3 down to -4 are represented in each of the three schemes:
| Number | Sign Magnitude | One's Complement | Two's Complement |
|---|---|---|---|
| 3 | 011 | 011 | 011 |
| 2 | 010 | 010 | 010 |
| 1 | 001 | 001 | 001 |
| 0 | 000 | 000 | 000 |
| -0 | 100 | 111 | 000 |
| -1 | 101 | 110 | 111 |
| -2 | 110 | 101 | 110 |
| -3 | 111 | 100 | 101 |
| -4 | --- | --- | 100 |
-0 creates a redundant bit pattern (100 and 111 respectively).-0 evaluates to 111 + 1 = 000 (ignoring carry out), making zero completely unique.-0 is eliminated, 2's Complement gains an extra negative value (-4 = 100) for 3 bits!Two's complement is so fundamental that you should be able to convert numbers instantly in your head using two techniques:
Example: Find 4-bit representation of -5.
+5 in 4 bits: 010110101010 + 1 = 1011Result: -5 = 10112
Scan the binary number from Right to Left (LSB to MSB):
0s and the first 1 unchanged.1.Example for +6 (0110): Copy 0, copy 1 $\rightarrow$ invert remaining 01 to 10 $\rightarrow$ 1010 (-6).
| Representation | N-Bit Range Formula | 4-Bit Range Example |
|---|---|---|
| Unsigned | 0 to 2N - 1 | 0 to 15 |
| Sign Magnitude | -(2N-1 - 1) to +(2N-1 - 1) | -7 to +7 |
| One's Complement | -(2N-1 - 1) to +(2N-1 - 1) | -7 to +7 |
| Two's Complement | -2N-1 to +(2N-1 - 1) | -8 to +7 |
Why did CPU designers standardize on Two's Complement? Two primary reasons:
In two's complement, subtraction is performed by adding the two's complement of the subtrahend:
A - B = A + (-B) = A + (~B + 1)
The ALU uses the exact same addition hardware for both A + B and A - B by feeding ~B into the adder and setting the carry-in bit to 1!
In software, comparing numbers for zero (e.g. if (x == 0)) requires only one bitwise zero test rather than checking for both +0 and -0.
Overflow occurs when the result of adding two numbers of the same sign exceeds the maximum range of $N$ bits (e.g., adding two positive numbers yields a negative result bit, or adding two negative numbers yields a positive result bit).
To convert an $N$-bit Two's complement number to a larger bit width (e.g., 4 bits to 8 bits), copy the MSB (sign bit) across all added bits. For instance, 4-bit 1011 (-5) becomes 8-bit 11111011 (-5).
Yes! Regardless of bit width $N$, -1 in Two's Complement is always represented as all 1s (e.g., 3-bit 111, 4-bit 1111, 8-bit 11111111, 32-bit 0xFFFFFFFF).