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.

  • Example: Widening an 8-bit unsigned integer to 16 bits:
    • 8-bit binary: 1001 1100 (Decimal value: 156)
    • 16-bit zero-extended: 0000 0000 1001 1100 (Decimal value: 156)

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.

  • Positive Value Example:
    • 8-bit signed binary: 0011 0100 (MSB is 0, Decimal value: +52)
    • 16-bit sign-extended: 0000 0000 0011 0100 (Decimal value: +52)
  • Negative Value Example:
    • 8-bit signed binary: 1111 0000 (MSB is 1, Decimal value: -16)
    • 16-bit sign-extended: 1111 1111 1111 0000 (Decimal value: -16)

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

  • Intended Data Type: Zero-extension is used for unsigned data types; sign-extension is used for signed two's complement data types.
  • Bit Filling Rule: Zero-extension always writes 0s to the new upper bits; sign-extension writes copies of the original MSB (0s for positive, 1s for negative).
  • Arithmetic Correctness: Choosing the wrong instruction leads to value corruption—zero-extending a negative number turns it into a large positive number, while sign-extending an unsigned number where the MSB is 1 mistakenly converts a large positive value into a negative value.