What is Sign extension?

Widening a signed integer to more bits by copying its top (sign) bit into all the new upper bits, which preserves its value. Filling the upper bits with zeros is zero extension.

Field: ISA & Assembly

Japanese name: 符号拡張

Why copying the sign bit preserves the value

In n-bit two's complement, the top bit carries a weight of minus 2 to the power n-1 and the others carry their usual positive weights. Extending a negative number by one bit adds a new top bit worth minus 2 to the power n and turns the old top bit into a positive 2 to the power n-1; together they still contribute minus 2 to the power n-1, so the value is unchanged. Repeating this for every added bit justifies the rule. For example, 4-bit 1010 (-6) becomes 8-bit 11111010, still -6.

Where ISAs sign-extend

Immediates are the most common case: RISC-V's 12-bit I-type immediate is sign-extended, so addi accepts -2048 to 2047 and negative constants work without a separate subtract instruction. Loads come in pairs, lb and lh sign-extend while lbu and lhu zero-extend. On RV64, lw sign-extends a 32-bit value to 64 bits and lwu zero-extends it, and word instructions such as addw sign-extend their 32-bit results. x86 provides movsx and movzx for the same purpose. In hardware, sign extension costs nothing more than wiring the top bit to all the new positions.

A classic pitfall: lui plus addi

To build a 32-bit constant, RISC-V code uses lui for the upper 20 bits and addi for the lower 12. Because addi sign-extends its immediate, a low part of 0x800 or more is treated as a negative number, its value minus 4096. Assemblers compensate by adding 1 to the upper part whenever bit 11 of the constant is set, and the %hi and %lo relocation operators do this automatically. Hand-written code that splits the constant naively produces a value that is off by exactly 4096, a bug that is easy to miss in testing.

Sign vs. zero extension in C

C chooses the extension from the source type: converting a signed char or int16_t to int sign-extends, while converting an unsigned type zero-extends. Plain char is signed on some platforms and unsigned on others, for example signed on x86 but unsigned on most Arm Linux systems, so a byte value of 0xFF may become -1 or 255 depending on the target. That difference causes bugs when bytes are compared with constants or used as array indices. Using unsigned char or uint8_t for raw bytes avoids the ambiguity.

Try it: questions about Sign extension

A byte in memory holds 0x80. Which pair correctly gives the 32-bit register value after loading it with RISC-V lb and with lbu?

  1. Alb gives 0x00000080 (128); lbu gives 0xFFFFFF80 (-128)
  2. Blb gives 0xFFFFFF80 (-128); lbu gives 0x00000080 (128)
  3. CBoth give 0x00000080 (128)
Show answer and explanation

Answer: B. lb gives 0xFFFFFF80 (-128); lbu gives 0x00000080 (128)

lb (load byte) treats the byte as signed and copies its top bit into the upper 24 bits (sign extension). 0x80 has a top bit of 1, so the result is 0xFFFFFF80, which is -128. lbu (load byte unsigned) fills the upper bits with zeros (zero extension), giving 128. When C code reads a signed char versus an unsigned char, the compiler chooses between these two instructions.

FAQ

What is the difference between sign extension and zero extension?

Sign extension fills the new upper bits with copies of the original top bit, preserving the value of a signed number. Zero extension fills them with zeros, preserving the value of an unsigned number.

Why does RISC-V addi sign-extend its immediate?

So that a single instruction can add negative constants, such as addi sp, sp, -16. The 12-bit field then covers -2048 to 2047 rather than only non-negative values.

How do I sign-extend a value in C?

Store it in a signed type of the original width, such as int8_t or int16_t, and assign it to a wider signed type; the conversion sign-extends automatically and compilers emit the matching sign-extending instruction.

Books on ISA & Assembly Ad

As an Amazon Associate I earn from qualifying purchases.

Semiconductor, Electronics and Computing Calculators

This page was drafted with AI and fact-checked by a separate AI review before publication. Our Editorial policy explains how we write and check content. If you spot an error, please tell us on our Contact page.

Back to the glossary