Zero-Extend vs Sign-Extend: Integer Widening

When working with binary data, widening an integer involves converting a value from a smaller bit-width to a larger bit-width, such as converting an 8-bit byte into a 16-bit word or a 32-bit integer. The fundamental difference between a zero-extend instruction and a sign-extend instruction lies in how the newly created higher-order bits are populated: zero-extension fills the extra bits entirely with zeros to preserve unsigned values, whereas sign-extension replicates the original value’s sign bit across the new bits to maintain the correct numeric value of signed two’s complement integers.

What is Zero-Extension?

Zero-extension (often abbreviated as MOVZX in x86 assembly) is designed specifically for unsigned integers. In an unsigned integer system, all bits contribute directly to the magnitude of the number, with no bit designated for a negative sign.

When an unsigned integer is widened, zero-extension appends binary zeros (0) to all the newly added higher-order positions. This ensures that the original magnitude of the number remains unchanged.

Because the upper bits are zeros, they add zero value to the binary weighting, keeping the unsigned decimal value identical.

What is Sign-Extension?

Sign-extension (often abbreviated as MOVSX in x86 assembly) is designed for signed integers represented in two’s complement format. In two’s complement, the most significant bit (MSB) acts as the sign bit: a 0 denotes a non-negative number, while a 1 denotes a negative number.

When widening a signed integer, sign-extension copies the original MSB into every newly added higher-order bit position.

In the negative example, filling the upper bits with 1s is necessary because the new MSB represents a larger negative weight (e.g., \(-2^{15}\) in a 16-bit integer). The series of leading 1s mathematically offsets this higher negative base to preserve the exact negative value of \(-16\). If zero-extension were applied to 1111 0000, it would result in 0000 0000 1111 0000, unintentionally converting \(-16\) into \(+240\).

Key Comparison