HP C A.06.05 Reference Manual

Expressions and Operators
Cast Operator
Chapter 5 99
Casting Integers to Other Integers
It is possible to cast one integer into an integer of a different size and to convert a
floating-point value, enumeration value or pointer to an integer. Conversions from one type of
integer to another fall into the following cases (A-E):
* Case C for long in 64-bit mode.
CASE A: Trivial Conversions It is legal to convert a value to its current type by casting it,
but this conversion has no effect.
CASE B: Integer Widening Casting an integer to a larger size is fairly straightforward.
The value remains the same, but the storage area is widened. The compiler preserves the sign
of the original value by filling the new leftmost bits with ones if the value is negative, or with
zeros if the value is positive. When it converts to an unsigned integer, the value is always
positive, so the new bits are always filled with zeros. The following table illustrates this
principle.
hex dec
char i = 37 55
(short) i => 0037 55
(int) i => 00000037 55
char j = c3 -61
(short) j => ffc3 -61
(int) j => ffffffc3 -61
Table 5-9 Integer Conversions
Original Type char short int
unsigne
d char
unsigne
d short
unsigne
d int
char ABBDEE
short CABCDE
int (long)* C C A* C C D*
unsigned char DBBABB
unsigned short CDBCAB
unsigned int CCDCCA