Byte Order Marks and Endianness
What you'll learn: How computers decide the order of bytes in multi-byte characters and what the Byte Order Mark (BOM) tells us about that order.
The Byte Order Problem
When you learned about UTF-16 and UTF-32, you saw that characters often need multiple bytes. But here's the catch: should those bytes be stored "left-to-right" or "right-to-left"? Different computer systems make different choices.
What is Endianness?
Endianness describes the order in which bytes are arranged in memory:
- Big-endian: Most significant byte first (like reading left-to-right)
- Little-endian: Least significant byte first (like reading right-to-left)
Analogy
Think of writing the year "2025":
- Big-endian: 2025 (thousands first)
- Little-endian: 5202 (ones first)
Both represent the same value, just in different orders.
The Byte Order Mark (BOM)
A BOM is a special invisible character placed at the very start of a text file to signal:
- Which encoding is used (UTF-8, UTF-16, or UTF-32)
- Which byte order to expect
For UTF-16, the BOM is the character U+FEFF:
- In big-endian: stored as
FE FF - In little-endian: stored as
FF FE
When a program opens the file, it reads these first bytes to know how to interpret everything that follows.
Why It Matters
Without knowing the byte order, multi-byte characters could be completely misinterpreted. The BOM acts like a decoder ring, telling the program: "Read my bytes in this order."
Key Takeaway: Endianness determines byte order in multi-byte encodings, and the Byte Order Mark (BOM) is a special marker that tells programs which order was used so characters display correctly.