Endianness of 16-Bit Integers in GIF Headers

This article explains the byte order used for 16-bit integer values in Graphics Interchange Format (GIF) file headers and descriptors. It covers the specific endianness mandated by the GIF specification, highlights the exact header fields affected, provides a byte-level example of how dimensions are stored, and details the implications for software developers parsing GIF files on different CPU architectures.

The GIF specification—encompassing both the original GIF87a and the updated GIF89a standards—explicitly dictates that all multi-byte numerical values are stored in little-endian byte order. In a little-endian format, the least significant byte (LSB) is stored at the lowest memory address (first in the byte stream), followed by the most significant byte (MSB).

In a GIF file, 16-bit unsigned integers appear immediately following the 6-byte signature and version header (GIF87a or GIF89a) within the Logical Screen Descriptor:

Similar 16-bit integers are also used later in the file within each local Image Descriptor:

Byte-Level Example

Consider an image with a width of 800 pixels and a height of 600 pixels:

In the raw binary stream of the Logical Screen Descriptor, these dimensions appear sequentially as:

20 03 58 02

Implementation Considerations

Because GIF was developed by CompuServe primarily for x86-based personal computers, its native little-endian layout matches the native memory layout of x86 and modern ARM architectures.

When writing a GIF parser or decoder: