HP C A.06.05 Reference Manual
Type Conversions
Arithmetic Conversions
Chapter 480
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.
When a long long (or long in the 64-bit data model) is converted into another integral data
type that is of shorter length, truncation may occur. When a long long is converted into a
double type, no overflow will occur, but there may be a loss of precision.
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.