Open System Services Porting Guide (G06.24+, H06.03+)

Table Of Contents
Porting From Specific UNIX Systems
Open System Services Porting Guide520573-006
9-10
Initializing Multiple-Word Entities in Chunks of
Multiple Bits
Initializing Multiple-Word Entities in Chunks of Multiple Bits
Use care when porting code that initializes multiple-word entities with specific bit
patterns. For example, on a little-endian system, the following array of two 32-bit
integer values can be used to initialize a 64-bit double entity:
u.ul[0] = 0x7fffffff;
u.ul[1] = 0xffffffff;
To produce the correct results on a big-endian system, the subscripts must be
reversed:
u.ul[1] = 0x7fffffff;
u.ul[0] = 0xffffffff;
To fix this problem, change initialization assignment statements so that bit patterns are
not used.
Using Bytes for More Than One Purpose
Sometimes code that is trying to make very efficient use of memory takes advantage of
the fact that some of the bytes in an integer often are not used. For example, if a
particular int field in a record will hold only values in the range 0 through 10,000,000,
the most significant byte will always contain zero. A 1-byte field could be stored in that
byte to make the record one byte smaller.
If the most significant byte is accessed through a character array or by casting and
dereferencing a pointer, then the code is not portable and slightly different versions are
needed on big-endian and little-endian systems. However, if bitwise operators are used
to mask, merge, and shift bytes, then the code is portable.
To fix this problem, change storage usage statements so that all data items occupy
unique locations.
Using Hexadecimal Constants as Byte Arrays
An endian problem occurs when a 32-bit value is treated both as a 32-bit value (an
integer) and as an array of four characters. For example, the following array is
equivalent to the number 0x11223344 on big-endian systems and the number
0x44332211 on little-endian systems:
char a[4] = {0x11, 0x22, 0x33, 0x44};
To fix this problem, change the program to properly preserve data typing.