Information Technology Reference
In-Depth Information
Implicit Conversions
For certain types of conversions, there is no possibility of loss of data or precision. For example,
it's easy to stuff an 8-bit value into a 16-bit type with no loss of data.
The language will do these conversions for you automatically. These are called
implicit
conversions
.
When converting from a source type with fewer bits to a target type with more bits, the
extra bits in the target need to be filled with either 0s or 1s.
When converting from a smaller unsigned type to a larger unsigned type, the extra, most
significant bits of the target are filled with 0s. This is called
zero extension
.
Figure 18-3 shows an example of the zero extension of an 8-bit value of 10 converted to a
16-bit value of 10.
Figure 18-3.
Zero extension in unsigned conversions
For conversion between signed types, the extra most significant bits are filled with the sign
bit of the source expression.
This maintains the correct sign and magnitude for the converted value.
This is ca led
sign extension
, and is illustrated in Figure 18-4, first with 10, and then
with -10.
Figure 18-4.
Sign extension in signed conversions