Negative Numbers in Hardware: Two's Complement
Digital circuits must process positive and negative values using binary voltages. This article analyzes three signed binary representations: sign-magnitude, one’s complement, and two’s complement.
Signed Number Constraints
An $n$-bit digital register stores $2^n$ unique bit combinations. For example, a 4-bit register stores 16 patterns (0000 through 1111). Signed systems divide these bit patterns between positive numbers, negative numbers, and zero.
Sign-Magnitude Representation
Sign-magnitude allocates the Most Significant Bit (MSB) as a sign indicator. A 0 indicates a positive value, and a 1 indicates a negative value. The remaining bits define the magnitude.
In a 4-bit sign-magnitude format:
| Bit Pattern | Represented Value |
|---|---|
| 0111 | +7 |
| 0001 | +1 |
| 0000 | +0 |
| 1000 | −0 |
| 1001 | −1 |
| 1111 | −7 |
Sign-magnitude presents two critical hardware limitations:
- Redundant Zero Representations: The format contains two zeros (
0000and1000). This redundancy complicates zero-detection logic in comparator circuits. - Complex Arithmetic Circuits: Adding values with opposing signs requires separate subtraction logic and magnitude comparators. Standard binary adders cannot process sign-magnitude data directly.
Sign-magnitude representation is primarily used in IEEE-754 floating-point formats rather than integer units.
One’s Complement Representation
One’s complement forms a negative value by inverting all bits of the corresponding positive value. For example, +5 (0101) inverts to −5 (1010).
Arithmetic addition requires an end-around carry. When an addition operation generates a carry-out bit from the MSB, the hardware must add this carry bit back into the LSB:
0011 (+3)
+ 1100 (-3)
------
1111 (-0 in one's complement)
One’s complement also contains two zero values (0000 and 1111). Furthermore, end-around carry logic increases critical path propagation delay in adders.
Two’s Complement Representation
Two’s complement defines a negative number $-N$ as the value that yields zero when added to $+N$ in modulo-$2^n$ arithmetic.
In an $n$-bit register:
$$-N = 2^n - N$$
For a 4-bit system ($2^4 = 16$), the negative representation of 5 evaluates to:
$$16 - 5 = 11 = 1011_2$$
Negation Algorithm
To negate a two’s complement number:
- Invert all bits (one’s complement).
- Add 1 to the least significant bit.
Example: Convert +5 (0101) to −5:
- Invert all bits:
1010 - Add 1:
1011
Hardware Addition Verification
Add +5 and −5 using a standard binary adder:
0101 (+5)
+ 1011 (-5)
------
10000
The 4-bit register truncates the fifth overflow bit, which leaves result 0000 (zero).
A standard binary adder executes signed and unsigned arithmetic without hardware modification. The interpretation of the bit pattern depends on software and RTL definitions.
Number Circle Representation
Figure 1 maps 4-bit two’s complement values in circular sequence. Clockwise progression increments values from 0000 (+0) to 0111 (+7). Adding 1 to 0111 transitions to 1000 (−8). Continuous clockwise progression increments negative values from 1001 (−7) to 1111 (−1), then wraps to 0000.
For an $n$-bit two’s complement system, the numerical range is:
$$-2^{n-1} \text{ to } +2^{n-1} - 1$$
| Bit Width ($n$) | Minimum Value (Negative) | Maximum Value (Positive) |
|---|---|---|
| 4 | −8 | +7 |
| 8 | −128 | +127 |
| 16 | −32,768 | +32,767 |
| 32 | −2,147,483,648 | +2,147,483,647 |
The range is asymmetric because zero occupies one of the non-negative bit representations. The minimum negative value (for example, −128 in an 8-bit system) cannot be negated within the same bit width.
Comparison of Signed Systems
| Decimal | Sign-Magnitude | One’s Complement | Two’s Complement |
|---|---|---|---|
| +7 | 0111 | 0111 | 0111 |
| +1 | 0001 | 0001 | 0001 |
| 0 | 0000 | 0000 | 0000 |
| −0 | 1000 | 1111 | None |
| −1 | 1001 | 1110 | 1111 |
| −5 | 1101 | 1010 | 1011 |
| −7 | 1111 | 1000 | 1001 |
| −8 | None | None | 1000 |
In Verilog, nets and registers evaluate as unsigned by default. To enable signed two’s complement operations, declare the signal with the signed keyword:
reg signed [7:0] data_reg;
Sign Extension
When expanding a signed value to a wider bit width, the hardware must perform sign extension. The circuit duplicates the MSB (sign bit) across all added upper bit positions.
8-bit signed value: 1011 0101 (-75)
Sign-extended to 16 bits: 1111 1111 1011 0101 (-75)
If a negative value is zero-extended instead of sign-extended, 10110101 (−75) converts to 0000000010110101 (+181), which introduces an arithmetic error.
Verilog applies zero-extension by default when assigning a narrower signal to a wider signal. To enforce sign extension, declare both variables as signed, or use system function $signed().
Practice Exercises
- Calculate the 8-bit two’s complement representation of decimal −19.
- Determine the decimal equivalent of 8-bit two’s complement value
11100110. - Sign-extend 4-bit binary value
1010to 8 bits.
View Solutions
- Binary +19 equals
00010011. Invert all bits (11101100) and add 1. Result:11101101. - The MSB is 1, so the number is negative. Invert all bits (
00011001) and add 1 (00011010= 26). Decimal value: −26. - The MSB is 1. Duplicate 1 across upper positions:
11111010. Both representations evaluate to decimal −6.