Open System Services Porting Guide (G06.29+, H06.06+, J06.03+)

Resolving the Endian Problem
The term “endian” refers to the order in which a computer numbers and stores its bytes. There are
two kinds of byte-endian ordering:
Big endian, in which the most significant byte of a multiple-byte entity (of a word, for example)
is stored first (at the lowest memory byte address or word boundary, represented as the leftmost
byte)
Little endian, in which the least significant byte is stored first (represented as the rightmost
byte)
Systems such as NonStop servers, IBM 360 and later mainframes, the IBM RS 6000, and Sun
Solaris use big-endian byte ordering. Systems based on the Intel x86 platform (such as Microsoft
Windows systems), HP OpenVMS systems, and HP Tru64 UNIX® on the Alpha architecture use
little-endian byte ordering.
The endian problem affects programs that are ported between platforms that use different byte-endian
ordering when those programs access data within a word by using byte addresses or bit locations.
For example:
If var1=256+32+3, its value is 291 or 0x00000123.
If the value of var1 is manipulated as a unit, then no problem exists. However, if the value is
manipulated by byte, then the most significant byte of var1 contains 23 on a little-endian system
but contains 00 on a big-endian system. This difference is important in the areas discussed in the
following subsections:
“Using Data Overlays” (page 168)
“Initializing Multiple-Word Entities in Chunks of Multiple Bits” (page 169)
“Using Bytes for More Than One Purpose” (page 169)
“Using Hexadecimal Constants as Byte Arrays” (page 169)
In general, these practices are discouraged in modern C programs and should not be used in
source code that you need to port. However, you should check for them.
Using Data Overlays
Data overlays are possible in many programming languages:
In C, type unions, type casting, and unbounded arrays can all cause data overlay.
In COBOL, the use of REDEFINES causes data overlay.
In TAL, the use of address equations, empty arrays, and unbounded arrays can all cause data
overlay.
For example, unions such as the following can result in an endian problem:
union int_byte {
int_val;
char byte[4];
};
union int_byte my_var;
my_var.int_val = 0x1000000;
if(my_var.byte[3] == 0)
printf("The number is divisible by 256\n");
On a big-endian system, this code works correctly; byte 3 is the least significant byte and contains
0 when my_var.int_val contains hexadecimal 1000000 and when that number is divided by
decimal 256. However, on a little-endian system, byte 3 is the most significant byte and contains
1 initially when my_var.int_val contains hexadecimal 1000000, even though hexadecimal 1000000
is evenly divisible by decimal 256.
To fix this problem, use code such as the following that is not sensitive to endian issues:
168 Porting From Specific UNIX Systems