<<< Subtraction of two 2's complement integers | Index | Overflow Condition >>> |
Moving 2's complement integers between data types of different sizes is illegal without the sign extension operation.
When necessary, you must sign-extend integer to convert signed value to a larger size.
Sign-extending means copying the sign bit of the unextended value to all bits on the left side of the larger-size value.
For example, 8-bit encoding of decimal number -56 can be sign-extended as follows:
11001000 <- 8-bit value of -56 11111111 11001000 <- extended to 16-bit value 11111111 11111111 11111111 11001000 <- extended 32-bit value
The same way as leading zeroes do not affect values of a positive numbers, leading 1s do not affect values of a negative numbers.
Sign-extend operation is often abbreviated SEXT.
SEXT widens the data while maintaining its sign and value.
Sign extending operation efficiently converts positive values as well, provided the sign bit is zero:
01001000 <- 8-bit value of 72 00000000 01001000 <- extended to 16-bit value 00000000 00000000 00000000 01001000 <- extended 32-bit value
Note: it is programmer's responsibility to keep track of which values are signed or unsigned, and treat them accordingly.
<<< Subtraction of two 2's complement integers | Index | Overflow Condition >>> |