Next: , Up: Machine-dependencies in Types Endianness

Endianness means the order in which the bytes of a value larger than one byte are stored in memory. This affects, e.g., integer values and pointers while, e.g., arrays of single-byte characters are not affected. The GPC String schema, however, contains Capacity and Length fields before the character array. These fields are integer values larger than one byte, so the String schema is affected by endianness.

Endianness depends on the hardware, especially the CPU. The most common forms are:

Note: There are processors which can run in both little-endian and big-endian mode, e.g. the MIPS processors. A single program, however, (unless it uses special machine code instructions) will always run in one endianness.

Under normal circumstances, programs do not need to worry about endianness, the CPU handles it by itself. Endianness becomes important when exchanging data between different machines, e.g. via binary files or over a network. To avoid problems, one has to choose the endianness to use for the data exchange. E.g., the Internet uses big-endian data, and most known data formats have a specified endianness (usually that of the CPU on which the format was originally created). If you define your own binary data format, you're free to choose the endianness to use.

To deal with endianness, GPC predefines the symbol __BYTES_LITTLE_ENDIAN__ on little-endian machines and __BYTES_BIG_ENDIAN__ on big-endian machines. Besides, the Run Time System defines the constant BytesBigEndian as False on little-endian machines and True on big-endian machines.

There are also the symbols __BITS_LITTLE_ENDIAN__, __BITS_BIG_ENDIAN__, __WORDS_LITTLE_ENDIAN__, __WORDS_BIG_ENDIAN__ and the constants BitsBigEndian and WordsBigEndian which concern the order of bits within a byte (e.g., in packed records) or of words within multiword-numbers, but these are usually less important.

The Run Time System also contains a number of routines to convert endianness and to read or write data from/to binary files in a given endianness, independent of the CPU's endianness. These routines are described in the RTS reference (see Run Time System), under endianness. The demo program endiandemo.pas contains an example on how to use these routines.