Signed Numbers: Two's Complement and Sign Extension
Write a negative number in two's complement, negate it, name its range, and widen it without changing its value.
You Will Be Able To
- Compare sign-magnitude, ones' complement and two's complement on four bits.
- Negate a two's-complement number by inverting and adding one.
- State the range of an N-bit signed number and its one value with no positive twin.
- Choose sign extension or zero extension when a value gets wider.
- Lesson
- G1.2
- Time
- 25 min
- Runs on
- Your browser
- Check
- 6 questions
A temperature sensor sends -3 as one byte. The logger copies the byte into a 16-bit register and records 253 degrees. No bit of the byte changed. The eight new bits were filled with zeros, and this value needed ones.
Weights show bit magnitudes. For signed values, the leftmost bit has a negative weight.
- Unsigned
- 253
- Two’s complement
- -3
- Hexadecimal
- 0xFD
- The sensor byte. Read as unsigned, the pattern is 253. Read as signed, the top bit weighs -128 and the value is -3.
- A positive value. With the top bit at 0, both readings agree: 3.
The bits do not say which reading is meant. The design must say it.
Three Ways to Write a Negative Number
Sign-magnitude uses the top bit as a sign and the other bits as the size. Ones' complement negates a number by inverting every bit. Two's complement inverts every bit and then adds one.
| Pattern | Sign-magnitude | Ones' complement | Two's complement |
|---|---|---|---|
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 | -0 | -7 | -8 |
1001 | -1 | -6 | -7 |
1010 | -2 | -5 | -6 |
1011 | -3 | -4 | -5 |
1100 | -4 | -3 | -4 |
1101 | -5 | -2 | -3 |
1110 | -6 | -1 | -2 |
1111 | -7 | -0 | -1 |
All sixteen 4-bit patterns. The two older systems each have a second zero. Two's complement uses that pattern for -8.
Two zeros cost a pattern and make "is it zero?" a two-way test. Addition is also harder. In ones' complement, a carry out of the top bit must be added back at the bottom. This is the end-around carry.
Add the patterns
5 + (-3) in ones' complement is 0101 + 1100. The 4-bit sum is 0001, with a carry out.
Add the carry back
0001 + 1 is 0010, which is 2. Without this step the answer would be 1.
Two's complement needs no such step, so nearly all hardware uses it. Ones' complement lives on in the Internet checksum of lesson G1.10.
Two's Complement on the Wheel
In two's complement the top bit has a negative weight. On four bits the weights are -8, 4, 2 and 1. So 1011 is -8 + 2 + 1, which is -5.
Weights show bit magnitudes. For signed values, the leftmost bit has a negative weight.
- Unsigned
- 5
- Two’s complement
- 5
- Plus five. The pattern 0101 is 5 in both readings.
- Invert every bit. Inverting gives 1010. Read as signed, that is -6: one step short of -5.
- Add one. Adding one gives 1011, which is -5. Invert and add one is negation.
- The edge of the wheel. This is +7, the largest value. One more step lands on 1000, which is -8.
Adding 1 always moves one step round the wheel. The signed reading jumps from +7 to -8 at one place only.
Negation is also "subtract from 2^N". On four bits, 16 - 5 is 11, and 11 is the pattern 1011. That is why two's complement is called the radix complement.
An adder does not care which reading you use. The same circuit adds unsigned and two's-complement numbers. Lesson G1.3 uses this.
The Range Is Lopsided
N bits in two's complement run from -2^(N-1) to 2^(N-1) - 1. There is one more negative value than positive values, because zero takes a pattern from the positive half.
| Bits | Smallest | Largest |
|---|---|---|
| 4 | -8 | 7 |
| 8 | -128 | 127 |
| 16 | -32,768 | 32,767 |
| 32 | -2,147,483,648 | 2,147,483,647 |
The range of a two's-complement number.
The most negative value has no positive twin. Negate -128 in a byte: invert 10000000 to get 01111111, then add one. The result is 10000000 again.
Sign Extension and Zero Extension
A value often moves into a wider register. The new bits on the left must be chosen so that the value stays the same.
- Neutral
- Data
- Zero extension
The new bits are 0. An unsigned value keeps its value: 253 stays 253.
- Sign extension
The new bits copy bit 7, the sign bit. A signed value keeps its value: -3 stays -3.
Read the diagram as text
- 11111101: the byte: -3, or 253
- 00000000 11111101: zero extension
- 11111111 11111101: sign extension
- 253: right for an unsigned byte
- -3: right for a signed byte
- 11111101 → 00000000 11111101: fill with 0
- 00000000 11111101 → 253: reads as
- 11111101 → 11111111 11111101: copy bit 7
- 11111111 11111101 → -3: reads as
Copies of the sign bit on the left never change a two's-complement value.
SystemVerilog picks the fill from the type of the source. A plain logic [7:0] is unsigned, so it is widened with zeros. A logic signed [7:0] is widened with copies of its top bit. $signed(x) reads an unsigned value as signed for one expression.
Zero-extended by accident
logic [7:0] raw;
logic signed [15:0] wide;
assign wide = raw;
Measured with raw at -3: wide is 00fd, which is 253.
Sign-extended
logic [7:0] raw;
logic signed [15:0] wide;
assign wide = $signed(raw);
Measured: wide is fffd, which is -3.
An expression is signed only when every operand is signed. One unsigned operand makes the whole operation unsigned. With a at -3 and an unsigned b at 1, a < b is 0, because -3 is read as 253.
Asked in interviews
How do you negate a two's-complement number?
Invert every bit and add one. The one exception is the most negative value, which negates to itself because its positive twin does not fit.
Why does hardware use two's complement?
It has one zero, and one adder works for signed and unsigned numbers. Sign-magnitude and ones' complement have two zeros and need extra steps to add.
What is the difference between
>>and>>>?>>fills the top with zeros.>>>on a signed value fills with copies of the sign bit. For a signed byte at -3,>>> 1gives -2 and>> 1gives 126.
Try It
The byte holds 11111101. Read as unsigned it is 253. Read as signed it is -3.
The plain assignment widens the unsigned byte with zeros, so zero_ext is 253. The assignment through $signed copies bit 7 into the new bits, so sign_ext is still -3.
Change the value to 8'sd3 and run. Both widened values are 3: for a value with a 0 sign bit, the two extensions agree. Then try -8'sd128.
raw 11111101 unsigned 253 signed -3 zero_ext 0000000011111101 253 sign_ext 1111111111111101 -3Check Yourself
Common Mistakes
Wrong Negate a number by flipping only its top bit.
That is sign-magnitude; in two's complement, 0101 with the top bit flipped is 1101, which is -3.
Fix Invert every bit and add one: 0101 becomes 1010, then 1011, which is -5.
n = {~n[3], n[2:0]}; // 0101 -> 1101 (-3)n = ~n + 4'd1; // 0101 -> 1011 (-5)Wrong Expect the negative of -128 to be +128 in a byte.
A signed byte ends at +127, so the negation wraps and gives -128 again.
Fix Use one more bit for the result, or treat the most negative value as a special case.
Wrong Assign an unsigned byte that holds a negative number to a wider variable.
An unsigned source is widened with zeros, so -3 becomes 253.
Fix Declare the source signed, or wrap it in $signed(), so that its top bit is copied.
wide = raw; // 00fdwide = $signed(raw); // fffdWrong Compare a signed value with an unsigned one, as in a < b.
One unsigned operand makes the whole comparison unsigned, so -3 is read as 253.
Fix Make both sides signed: a < $signed(b).
Remember
- Two's complement gives the top bit a negative weight; it has one zero and needs no special adder.
- To negate, invert every bit and add one.
- N bits run from -2^(N-1) to 2^(N-1) - 1; the most negative value negates to itself.
- Widen a signed value with copies of its sign bit and an unsigned value with zeros.
- In SystemVerilog one unsigned operand makes the whole expression unsigned; use
signedor$signed.