[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

#### 8.2.11.1 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:

- Little-endian

  Little-endian machines store the least significant byte on the lowest memory address (the word is stored little-end-first).

  E.g., if the 32 bit value `$deadbeef` is stored on memory address `$1234` on a little-endian machine, the following bytes will occupy the memory positions:

  Address | Value
  `$1234` | `$ef`
  `$1235` | `$be`
  `$1236` | `$ad`
  `$1237` | `$de`

  Examples for little-endian machines are IA32 and compatible microprocessors and Alpha processors.

- Big-endian

  Big-endian machines store the most significant byte on the lowest memory address (the word is stored big-end-first).

  E.g., if the 32 bit value `$deadbeef` is stored on memory address `$1234` on a big-endian machine, the following bytes will occupy the memory positions:

  Address | Value
  `$1234` | `$de`
  `$1235` | `$ad`
  `$1236` | `$be`
  `$1237` | `$ef`

  Examples for big-endian machines are the Sparc and Motorola m68k CPU families and most RISC processors. Big-endian byte order is also used in the Internet protocols.

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 section 8.13 Pascal declarations for GPC's Run Time System), under ``endianness'`. The demo program ``endiandemo.pas'` contains an example on how to use these routines.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

This document was generated by Frank Heckenbach on May, 10 2002 using texi2html