Sign extension

Sign extension (sometimes abbreviated as sext, particularly in mnemonics) is the operation, in computer arithmetic, of increasing the number of bits of a binary number while preserving the number's sign (positive/negative) and value. This is done by appending digits to the most significant side of the number, following a procedure dependent on the particular signed number representation used.

For example, if six bits are used to represent the number " " (decimal positive 10) and the sign extends operation increases the word length to 16 bits, then the new representation is simply " ". Thus, both the value and the fact that the value was positive are maintained.

If ten bits are used to represent the value " " (decimal negative 15) using two's complement, and this is sign extended to 16 bits, the new representation is " ". Thus, by padding the left side with ones, the negative sign and the value of the original number are maintained.

In the Intel x86 instruction set, for example, there are two ways of doing sign extension:
 * using the instructions,  ,  , and  : convert the byte to word, word to doubleword, word to extended doubleword, and doubleword to quadword, respectively (in the x86 context a byte has 8 bits, a word 16 bits, a doubleword and extended doubleword 32 bits, and a quadword 64 bits);
 * using one of the sign extended moves, accomplished by the   ("move with sign extension") family of instructions.

Zero extension
A similar concept is zero extension (sometimes abbreviated as zext). In a move or convert operation, zero extension refers to setting the high bits of the destination to zero, rather than setting them to a copy of the most significant bit of the source. If the source of the operation is an unsigned number, then zero extension is usually the correct way to move it to a larger field while preserving its numeric value, while sign extension is correct for signed numbers.

In the x86 and x64 instruction sets, the  instruction ("move with zero extension") performs this function. For example,  copies a byte from the   register to the low-order byte of   and then fills the remaining bytes of   with zeroes.

On x64, most instructions that write to the entirety of lower 32 bits of any of the general-purpose registers will zero the upper half of the destination register. For example, the instruction  will clear the upper 32 bits of the   register.