Sign extension (sometimes abbreviated as sext, particularly in
mnemonic
A mnemonic device ( ), memory trick or memory device is any learning technique that aids information retention or retrieval in the human memory, often by associating the information with something that is easier to remember.
It makes use of e ...
s) is the operation, in
computer arithmetic
Computer arithmetic is the scientific field that deals with representation of numbers on computers and corresponding implementations of the arithmetic operations.
It includes:
*Fixed-point arithmetic
*Floating-point arithmetic
*Interval arithmet ...
, of increasing the number of
bit
The bit is the most basic unit of information in computing and digital communication. The name is a portmanteau of binary digit. The bit represents a logical state with one of two possible values. These values are most commonly represented as ...
s of a
binary number
A binary number is a number expressed in the Radix, base-2 numeral system or binary numeral system, a method for representing numbers that uses only two symbols for the natural numbers: typically "0" (zero) and "1" (one). A ''binary number'' may ...
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
In computing, signed number representations are required to encode negative numbers in binary number systems.
In mathematics, negative numbers in any base are represented by prefixing them with a minus sign ("−"). However, in RAM or CPU reg ...
used.
For example, if six bits are used to represent the number "
00 1010
" (decimal positive 10) and the sign extends operation increases the
word length
In computing, a word is any processor design's natural unit of data. A word is a fixed-sized datum handled as a unit by the instruction set or the hardware of the processor. The number of bits or digits in a word (the ''word size'', ''word wid ...
to 16 bits, then the new representation is simply "
0000 0000 0000 1010
". Thus, both the value and the fact that the value was positive are maintained.
If ten bits are used to represent the value "
11 1111 0001
" (decimal negative 15) using
two's complement
Two's complement is the most common method of representing signed (positive, negative, and zero) integers on computers, and more generally, fixed point binary values. Two's complement uses the binary digit with the ''greatest'' value as the ''s ...
, and this is sign extended to 16 bits, the new representation is "
1111 1111 1111 0001
". Thus, by padding the left side with ones, the negative sign and the value of the original number are maintained.
In the
Intel
Intel Corporation is an American multinational corporation and technology company headquartered in Santa Clara, California, and Delaware General Corporation Law, incorporated in Delaware. Intel designs, manufactures, and sells computer compo ...
x86 instruction set
The x86 instruction set refers to the set of instructions that x86-compatible microprocessors support. The instructions are usually part of an executable program, often stored as a computer file and executed on the processor.
The x86 instructi ...
, for example, there are two ways of doing sign extension:
* using the instructions
cbw
,
cwd
,
cwde
, and
cdq
: 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
movsx
("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
movzx
instruction ("move with zero extension") performs this function. For example,
movzx ebx, al
copies a byte from the
al
register to the low-order byte of
ebx
and then fills the remaining bytes of
ebx
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
mov eax, 1234
will clear the upper 32 bits of the
rax
register.
See also
*
Arithmetic shift
In computer programming, an arithmetic shift is a shift operator, sometimes termed a signed shift (though it is not restricted to signed operands). The two basic types are the arithmetic left shift and the arithmetic right shift. For binary ...
and
logical shift
In computer science, a logical shift is a bitwise operation that shifts all the bits of its operand. The two base variants are the logical left shift and the logical right shift. This is further modulated by the number of bit positions a given v ...
References
* Mano, Morris M.; Kime, Charles R. (2004). ''Logic and Computer Design Fundamentals'' (3rd ed.), pp 453. Pearson Prentice Hall. .
Notes
{{Notelist, 30em
Binary arithmetic
de:Zweierkomplement#Vorzeichenerweiterung
ru:Дополнительный код (представление числа)#Расширение знака