Sign Extension and Changing Widths
To widen a signed number, copy its top bit into all the new places; to widen an unsigned one, fill them with zeros. Both keep the value exactly. Narrowing goes the other way: chopping off the high bits keeps the value mod , which is the same number only if it fit.
What the symbols mean
- Sign bit
The top bit of the narrow number, copied into every new place.
- New width
The wider register's size, m > n.
Explanation
Sign extension is simply how a computer moves a signed number into a wider register without changing what it means: copy the sign bit (the top bit) into every new bit on the left. The name is literal: you extend the sign out to the new width. Zero extension is the unsigned version, which fills the new bits with 0s, and truncation is the trip back down, keeping only the low bits. The formula with the brace looks fussy, but it only says "write the top bit more times, then the old bits." The proof that it works is a three-line geometric series; bear with it.
Why widths change at all. Registers are 32 or 64 bits, but data often comes in smaller pieces: bytes from a file, 16-bit samples from a sensor, 12-bit readings from a converter. Loading one into a register means widening it. And here the lesson from how a computer knows a number is negative comes back: the same byte has to be widened differently depending on how it's read. That's why there are two load instructions, lbu (zero-extend) and lb (sign-extend) on RISC-V, movzx and movsx on x86, and why C picks one from the type when it widens an int8_t or a uint8_t to int.
Zero extension keeps unsigned values. The new bits are 0s, and 0s add nothing to . Easy.
Sign extension keeps signed values. If the top bit is 0, sign extension is the same as zero extension, and we're done. So suppose it's 1. In bits the top bit is worth . After extending to bits, places through are all 1s, and in the wider number they're worth
The series adds up to (it's ). So the 1s are worth , exactly what the old sign bit was worth on its own. The low bits didn't move, so the value didn't change.
There's an even shorter way to see it, one bit at a time. Add one new copy of the sign bit: the new top bit is worth , and the old top bit, no longer on top, is now worth . Together that's , the same as before. Repeat, and induction takes you to any width.
For example, is . Sign-extended to 8 bits it's , and the five 1s on top are worth , the same as the old sign bit. Still . There's an animation further down that runs through all sixteen 4-bit patterns this way.
Truncation is reduction mod . Narrowing just drops the high bits. Write : every dropped bit was worth a multiple of , so what's left is , the remainder from modular arithmetic. Read unsigned, that's the value itself only if . Read signed, it's the one number in to that's congruent to mod , which you can compute as
So truncation keeps a signed value exactly when it fit in the narrow range. In bit terms: exactly when every dropped bit was a copy of the new top bit, which is the same as saying the wide number was the sign extension of the narrow one.
A common mistake. It's tempting to widen a negative number by padding it with zeros on the left, since that's what you'd do with a decimal number (0042 is still 42). For signed binary it's wrong: () padded to becomes . The zeros take away the top bit's negative weight. The reverse mistake is just as real: sign-extending an unsigned byte like 0xFB turns into 0xFFFB, which is unsigned.
See it
Worked examples
Sign-extend the 8-bit value to 16 bits, and check that the value didn't change.
- 1
First read the 8-bit value. The top bit is 1, so it's worth : .
A 32-bit int holds . A C program stores it into an int8_t, a signed byte. What value comes out?
- 1
Storing into a byte keeps only the low 8 bits. Keeping the low 8 bits is the same as taking the remainder mod .
Practice
Type your answer and check it. Solve them all to complete the lesson.
Which is the 8-bit sign extension of the 4-bit pattern ?
The byte 0xC4 is loaded into a 32-bit register. What does the register hold after a zero-extending load (read unsigned), and after a sign-extending load (read signed)? Enter both, in that order.
A 32-bit value is stored into a uint16_t (an unsigned 16-bit integer). What value is stored?
A 32-bit value is stored into an int16_t (a signed 16-bit integer). What value comes out?
Which of these 16-bit signed values does NOT survive being truncated to a signed byte?
An accelerometer sends a 12-bit two's complement reading in the low 12 bits of a 16-bit word, with the top 4 bits 0. Each count is 1 milli-g. The firmware receives 0x0F38 and sign-extends it with (int16_t)(w << 4) >> 4, which moves the 12-bit sign bit up to bit 15 and then shifts back down with an arithmetic shift. What acceleration does it get, in milli-g?
Why does copying the sign bit keep a negative value the same when you widen it from to bits?
A signed byte holds . By mistake it's zero-extended to 16 bits, and the result is read as a signed 16-bit number. What value does the program see?
The hardest one here. Get it, and you've got this lesson.
A 16-bit signed value is truncated to 8 bits and then sign-extended back to 16 bits. For how many of the possible values of does this round trip give back ? And what does come back as? Enter both.
Where it's used
Every load of a byte or a half-word into a register is a widening, so instruction sets come with both flavors (lb/lbu, lh/lhu) and compilers choose by type. Sensor drivers sign-extend 10-, 12- and 24-bit readings by hand, often with the shift-up, shift-down trick, and audio code truncates 32-bit mixing results back to 16-bit samples, which is why loud mixes clip or wrap unless the code clamps first.