Big endian and little endian are two different byte ordering methods used in computing. Specifically, big endian and little endian refer to the order in which bytes are stored in computer memory. The terms come from Jonathan Swift’s 1726 satirical novel Gulliver’s Travels, in which a war breaks out over whether soft boiled eggs should be opened at the big end or the little end.
In computing, big endian means the most significant byte is stored first (at the lowest memory address). Little endian is the opposite – the least significant byte is stored first. Let’s look at an example to understand this better.
Big Endian vs Little Endian Example
Consider the 4-byte hexadecimal number 0x12345678. In big endian format, it would be stored in memory as: Address Value 0x00 0x12 0x01 0x34 0x02 0x56 0x03 0x78
As you can see, the most significant byte 0x12 is stored first at the lowest address 0x00. In little endian format, the same number would be stored as: Address Value 0x00 0x78 0x01 0x56 0x02 0x34 0x03 0x12
Here, the least significant byte 0x78 is stored first. When reading a big endian number from memory, the bytes are read in increasing address order. For a little endian number, the bytes are read in decreasing address order.
Why Byte Ordering Matters
The byte ordering used for storage in memory and transmission across networks can have a big impact on programs. Consider a program that reads a 4-byte integer from memory. If the program assumes big endian ordering, but the number is actually stored in little endian format, the program will read the bytes in the wrong order and get the wrong value.
To avoid such issues, programmers must know the endianess used by their systems and account for it appropriately in their code. Otherwise, data corruption can occur when numbers are interpreted incorrectly.
Big Endian Systems
Some CPU architectures use big endian as their native byte ordering. Notable big endian systems include:
- SPARC
- PowerPC
- MIPS
- IBM System/360 and z/Architecture
- DEC Alpha
For software portability, big endian systems also support little endian data when required. But data in memory and network packets use big endian by default.
Little Endian Systems
Many popular CPU architectures use little endian as their native byte order. Some examples are:
- x86
- ARM
- AVR
- VAX
- IA-64
On these systems, little endian is the default for memory storage and data transmission. But they can read/write big endian data when needed for compatibility.
Bi-Endian Systems
Some architectures can switch between big endian and little endian modes dynamically. For example:
- ARM – Can switch between big and little endian with a status bit.
- PowerPC – Has instructions for loading/storing data in either endian format.
- MIPS – Supports a runtime config option to choose endianess.
This flexibility allows the same CPU to work with different types of data and software. The endian mode can be chosen to match external systems/peripherals as needed.
Network Byte Order
For data transmission, the network byte order standard must be used. This ensures compatibility between systems using different endian formats.
The network byte order is defined as big endian by RFC 1700. So all data sent over the network uses big endian, regardless of the native format of the source and destination systems.
This prevents data corruption when communicating between little endian and big endian machines. However, programmers must still convert local data representations to/from network byte order before transmission.
Converting Endianess
Changing a data value between big endian and little endian is straightforward:
- Separate the data into individual bytes (each byte is 8 bits).
- Reverse the order of the bytes.
- Reconstruct the data value from the reversed bytes.
Many compilers provide built-in functions like htonl(), htons(), ntohl() and ntohs() to convert 32-bit and 16-bit integers between host and network byte order. Programmers can also write their own byte swapping routines to handle endian conversion.
Detecting Endianess at Runtime
Some techniques allow programs to detect the system’s endian format at runtime. This enables the software to handle either big endian or little endian data automatically. Some examples:
- Check a known constant value – Compare expected vs actual byte order.
- Use unions to view the same data with different byte alignments.
- Check behavior of unaligned memory accesses (generating faults on some systems).
- Use special CPU instructions if available (e.g. ARM EABI).
With runtime detection, the program can adjust to the underlying system’s endianess without having to hardcode for one specific format.
Summary
Big endian and little endian define the order in which bytes are arranged in computer memory. They affect how programs interpret data values. Big endian stores the most significant byte first while little endian stores the least significant byte first. Many CPU architectures use one endian format natively but support reading/writing the opposite format when required. Network transmission always uses big endian byte order. Developers must be aware of endianess concerns to avoid data corruption issues.