Sign Extension
Widening a signed number from N to M bits without changing its value.
Problem
How do you widen an N-bit signed number to M bits (M > N) so it keeps the same value?
How it works
In two’s complement, the MSB carries the sign. To extend from N to M bits, copy the sign bit into every new upper bit position. The value is unchanged because each copied bit adds 0 net weight.
Example: extend 4-bit −3 (
For unsigned numbers, the equivalent is zero extension: pad the upper bits with 0. The choice between sign- and zero-extension is why ISAs have separate instructions (e.g.,
Example: extend 4-bit −3 (
1101) to 8 bits → 11111101. The four new MSBs are all 1 (the sign). Evaluating: −128+64+32+16+8+4+0+1 = −3. Same value.For unsigned numbers, the equivalent is zero extension: pad the upper bits with 0. The choice between sign- and zero-extension is why ISAs have separate instructions (e.g.,
MOVSX vs MOVZX in x86).Key insightSign extension is just copying the MSB upward. It’s free in hardware: literally wires, no gates needed.