sign extension
Sign extension is how you widen a signed number into more bits — say copying an 8-bit value into a 16-bit slot — without changing what it means. The obvious idea, padding the new high bits with zeros, only works for positive numbers. For negatives in two's complement it would be wrong. The fix is simple and elegant: copy the original sign bit (the topmost bit) into every new bit on the left. Picture stretching a banner: you do not paint the new fabric blank, you continue whatever colour the edge already was.
Here is why it works. In two's complement the value of -5 in 8 bits is 1111 1011. To make it 16 bits you copy the sign bit (a 1) into all eight new high positions, giving 1111 1111 1111 1011. Read that with place values and it is still -5, because the new -2^15 weight is exactly cancelled by the added 2^14 + 2^13 + ... + 2^8 ones. A positive number works the same way with a 0 sign bit: 8-bit +5 (0000 0101) extends to 0000 0000 0000 0101, still +5. So you always replicate the top bit — 0 for positives, 1 for negatives — and the value is preserved.
This matters constantly inside a processor. When an instruction loads a small signed value, like a byte or a short immediate, into a wider register, the hardware sign-extends it so the arithmetic comes out right. Two honest cautions: first, sign extension is only correct for signed values — unsigned values must be zero-extended (pad with 0s), and using the wrong one silently corrupts the number; second, this is the widening direction. Narrowing (truncating to fewer bits) is the dangerous opposite, since it can throw away meaningful bits and turn a large value into a wrong small one.
Sign-extend 8-bit -5 (1111 1011) to 16 bits by copying the sign bit 1: result 1111 1111 1111 1011, still -5. Sign-extend 8-bit +5 (0000 0101) by copying 0: result 0000 0000 0000 0101, still +5.
Replicate the top bit into the new high positions to widen a signed value safely.
Sign-extend signed values, zero-extend unsigned ones — picking the wrong widening silently corrupts the number. This is the opposite of truncation, which can lose meaningful bits and is the lossy direction.