HP C/iX Reference Manual (31506-90011)

52 Chapter4
Type Conversions
Arithmetic Conversions
Arithmetic Conversions
In general, the goal of conversions between arithmetic types is to maintain the same
magnitude within the limits of the precisions involved. A value converted from a less
precise type to a more precise type and then back to the original type results in the same
value.
Integral Conversions
A particular bit pattern, or representation, distinguishes each data object from all others of
that type. Data type conversion can involve a change in representation.
When signed integer types are converted to unsigned types of the same length, no change
in representation occurs. A short int value of -1 is converted to an unsigned short int value
of 65535.
Likewise, when unsigned integer types are converted to signed types of the same length,
no representational change occurs. An unsigned short int value of 65535 converted to a
short int has a value of -1.
If a signed int type is converted to an unsigned type that is wider, the conversion takes
(conceptually) two steps. First, the source type is converted to a signed type with the same
length as the destination type. (This involves sign extension.) Second, the resulting signed
type is converted to unsigned. The second step requires no change in representation.
If an unsigned integer type is converted to a signed integer type that is wider, the unsigned
source type is padded with zeros on the left and increased to the size of the signed
destination type.
In general, conversions from wide integer types to narrow integer types discard high-order
bits. Overflows are not detected.
Conversions from narrow integer types to wide integer types pad on the left with either
zeros or the sign bit of the source type as described above.
A "plain" char is treated as signed.
A "plain" int bit-field is treated as signed.
Floating Conversions
When an integer value is converted to a floating type, the result is the equivalent
floating-point value. If it cannot be represented exactly, the result is the nearest
representable value. If the two nearest representable values are equally near, the result is
the one whose least significant bit is zero.
When floating-point types are converted to integral types, the source type value must be in
the representable range of the destination type or the result is undefined. The result is the
whole number part of the floating-point value with the fractional part discarded as shown
in the following examples:
int i;
i = 9.99; /* i gets the value 9 */