# Understanding Endianness: Little vs Big-Endian and its Impact on data storage and transmission

In computer systems, data is represented using binary digits (bits), which are organized into groups of 8 bits called bytes. The most significant bit (MSB) is located at the leftmost side of a byte, while the least significant bit (LSB) is at the rightmost end. This arrangement follows a standard across all computer systems.

When writing data, whether it’s to memory, disk, or over a network, the bytes need to be stored. Regardless of the location or method of writing, the standard dictates that the LSB is stored at the rightmost side, and the MSB is stored at the leftmost side. This standard applies uniformly.

## Why Endianness Matters

However, complications arise when dealing with multiple bytes. For example, consider a 32-bit integer, which consists of 4 bytes. The order in which these bytes are written becomes crucial. There are two possibilities: either writing the most significant byte first (big endian) or writing the least significant byte first (little endian).

Let’s take an example of a 4-byte number with the hexadecimal representation `10203040`. In big endian, the bytes would be written in the order `10 20 30 40`, whereas in little endian, they would be written as `40 30 20 10`. The interpretation of the number depends on the byte order.

The challenge arises when two computers with different byte orders need to communicate with each other. If data written in big endian is interpreted as little endian, or vice versa, the program’s correctness is compromised. This misinterpretation of numbers can lead to significant problems, especially considering the vast amount of data transferred over the internet.

There are several ways to address the indianness issue. One approach is to include a header indicating the byte order explicitly. The header can contain a simple one-bit flag, where 0 represents little endian and 1 represents big endian. This way, the receiver knows how to interpret the data correctly.

Another approach is for both parties involved in the communication to agree on a common byte order. They can establish this agreement when establishing the connection or through a formal specification. By following a predefined byte order, they can ensure consistent interpretation of data.

Additionally, custom encoders and decoders can be developed to handle specific byte orders. By creating and shipping these specialized tools, the developers can control how the data is written and interpreted.

A fascinating solution to the endianness problem is byte order masking, used extensively in Unicode. A “magic number” of two bytes, `feff`, is attached as a marker at the beginning of the data. When received, the order of the bytes can determine the byte order used. If the marker is interpreted as `fffe`, it indicates a reverse byte order. This technique allows machines with different byte orders to communicate effectively.

Given the preferences and advantages of different byte orders, standardizing a single byte order for all systems is not feasible. Instead, it is essential to understand and handle endianness appropriately in practical scenarios. Writing code that deals with different byte orders can provide valuable insights and help developers grasp the complexities involved.

## Endianness in Dates

It’s worth noting that endianness is not limited to numerical representations. Even in date formats, there are variations in byte order conventions across different cultures and systems. For example, some use the day, month, year order (little endian), while others use the month, day, year order (big endian). The diversity in endianness further highlights the challenges in standardization.

## Conclusion

In conclusion, understanding the concept of endianness is crucial when dealing with data storage, transmission, and communication between computers. The choice between big endian and little endian byte order can have significant implications for the interpretation and accuracy of data. While there is no standardized approach across all systems, various solutions such as headers, agreed byte order, and specific encoders/decoders can mitigate the challenges of endianness. By grasping the complexities and practicalities of endianness, developers and engineers can effectively navigate this issue and ensure seamless data exchange in an interconnected world.

Here's the video ⤵

### Courses I teach

Alongside my daily work, I also teach some highly practical courses, with a no-fluff no-nonsense approach, that are designed to spark engineering curiosity and help you ace your career.

## System Design Masterclass

A no-fluff masterclass that helps SDE-2, SDE-3, and above form the right intuition to design and implement highly scalable, fault-tolerant, extensible, and available systems.

## System Design for Beginners

An in-depth and self-paced course for absolute beginners to become great at designing and implementing scalable, available, and extensible systems.

## Redis Internals

A self-paced and hands-on course covering Redis internals - data structures, algorithms, and some core features by re-implementing them in Go.

Writings and Learnings
Legal and Contact
Everything Else