Quiz Entry - updated: 2026.07.14
What is zero extension and how does it differ from sign extension?
Zero extension widens an unsigned value by filling the new upper bits with zeros, whereas sign extension copies the sign bit.
For an unsigned value, the high bits genuinely are zero, so padding with zeros is correct:
unsigned char c = 255; // 0xFF
unsigned int i = c; // should be 0x000000FF (255)
The movz family handles it:
| Instruction | Operation |
|---|---|
movzbq %al, %rax |
zero-extend byte → quad |
movzwq %ax, %rax |
zero-extend word → quad |
(no movzlq) |
use movl %eax, %eax instead |
There's a neat shortcut for the 32→64 case: any 32-bit operation already zero-extends into the full 64-bit register, so a plain movl %eax, %eax zeros the top half.
movzbl $0x80, %eax # 128 (0x00000080) — zero-extend
movsbl $0x80, %eax # -128 (0xFFFFFF80) — sign-extend
Tip: the same byte 0x80 becomes 128 or −128 depending purely on which extension you choose — movz for unsigned, movs for signed.
Go deeper:
Felix Cloutier — MOVZX — the movz-family reference confirming zero-fill of upper bits.
Sign extension (Wikipedia) — the companion article contrasting zero vs sign extension.