← Back to list

Understanding Endianness in Computing (With Detailed Examples)

If you’ve ever worked with low-level programming, networking, file formats, or binary data, you’ve probably encountered the term…

Aniket Ojha · 2026-02-22 11:50 · 0 claps · 3.1 min read
#operating-systems #socket-programming #data-storage #endianness
Open on Medium ↗
Wiki topics: RAG · RAG & Retrieval 💻 · Programming 🔒 · Cybersecurity

Understanding Endianness in Computing (With Detailed Examples)

If you’ve ever worked with low-level programming, networking, file formats, or binary data, you’ve probably encountered the term endianness. It sounds abstract — maybe even philosophical — but it’s actually a very practical concept that determines how computers store and interpret data.

In this article, we’ll break down:

  • What endianness is ?
  • The difference between little-endian and big-endian

. Real-world examples with memory diagrams

  • How to handle it in code

Let’s dive in.

🧠 What Is Endianness?

Endianness refers to the byte order used to represent multi-byte data types (like integers and floats) in memory.

Computers store data in bytes (8 bits). But many data types — such as 32-bit integers — take up multiple bytes. The question is:

In what order should those bytes be stored in memory?That ordering is what we call endianness.

🔢 A Simple Example: Storing a 32-bit Integer

Let’s take this 32-bit hexadecimal value:

0x12345678

This value consists of 4 bytes:

ByteValue10x1220x3430x5640x78

Now the question becomes:

👉 Which byte goes at the lowest memory address?

There are two answers.

🟢 Big-Endian

In big-endian systems:

The most significant byte (MSB) is stored at the lowest memory address.

Memory layout:

AddressValue0x10000x120x10010x340x10020x560x10030x78

So the number appears in memory exactly as we write it.

Think of it like reading numbers left to right.

🔵 Little-Endian

In little-endian systems:

The least significant byte (LSB) is stored at the lowest memory address.

Memory layout:

AddressValue0x10000x780x10010x560x10020x340x10030x12

The bytes are reversed in memory compared to how we write the number.

📊 Visual Memory Diagram

For the value 0x12345678:

Big-endian:
Address →   0x1000   0x1001   0x1002   0x1003
            0x12     0x34     0x56     0x78
Little-endian:
Address →   0x1000   0x1001   0x1002   0x1003
            0x78     0x56     0x34     0x12

🧪 Real Code Example in C

Let’s test endianness using C:

#include <stdio.h>
int main() {
    unsigned int x = 0x12345678;
    unsigned char *c = (unsigned char*)&x;
    printf("Byte 0: 0x%x\n", c[0]);
    printf("Byte 1: 0x%x\n", c[1]);
    printf("Byte 2: 0x%x\n", c[2]);
    printf("Byte 3: 0x%x\n", c[3]);
    return 0;
}

Output on a Little-Endian Machine (like most modern PCs):

Byte 0: 0x78
Byte 1: 0x56
Byte 2: 0x34
Byte 3: 0x12

This shows that most desktop CPUs (like Intel and AMD) use little-endian format.

🌍 Why Does Endianness Matter?

Endianness becomes critical when:

1️⃣ Networking (Big-Endian Standard)

Internet protocols use network byte order, which is big-endian.

For example:

  • TCP/IP headers
  • UDP packets
  • IP addresses

That’s why functions like this exist in socket programing:

htonl()  // host to network long
ntohl()  // network to host longexist in socket programming.

2️⃣ File Formats

Binary file formats may specify a fixed byte order.

Examples:

  • Some image formats
  • Executable formats
  • Database storage formats

If you misinterpret endianness while reading a file, the values become incorrect.

Example:

Reading 0x12345678 incorrectly as little-endian instead of big-endian would give:

0x78563412

Completely different number.

3️⃣ Cross-Platform Systems

Suppose:

  • System A (little-endian) sends binary data
  • System B (big-endian) receives it

Without conversion, the data will be corrupted.

🔬 Example: 16-bit Integer Breakdown

Let’s say we store:

0xABCD

Bytes:

  • 0xAB (MSB)
  • 0xCD (LSB)

Big-endian:

AddressValue0x20000xAB0x20010xCD

Little-endian:

AddressValue 0x20000xCD0x20010xAB

🏗 How CPUs Handle Endianness

Most modern architectures:

  • x86 / x86–64 → Little-endian
  • ARM → Usually little-endian (configurable in some cases)

Some architectures support both (called bi-endian).

🧩 Mixed or Middle Endian?

Historically, some rare systems used strange formats like:

0x12345678 → stored as 0x34 0x12 0x78 0x56

These are mostly obsolete and not used in modern systems.

🛠 Python Example

Python allows you to explicitly control byte order:

x = 0x12345678
# Convert to bytes (little-endian)
little = x.to_bytes(4, byteorder='little')
print(little)
# Convert to bytes (big-endian)
big = x.to_bytes(4, byteorder='big')
print(big)

Output:

b'xV4\x12'
b'\x124Vx'

🧮 Floating Point Endianness

Endianness applies to:

  • Integers
  • Floats
  • Doubles
  • Structs

Example (IEEE 754 float):

The bytes of a float are reordered the same way as integers.

This is why binary serialization frameworks must define byte order explicitly.

🚨 Common Debugging Nightmare

Imagine reading 4 bytes from a network packet:

00 00 01 00

Big-endian interpretation:

256

Little-endian interpretation:

65536

Huge difference. Same bytes. Different meaning.

🧠 Why Do Little-Endian Systems Exist?

Little-endian has certain implementation advantages:

  • Easier arithmetic operations
  • Easier type casting between smaller and larger integers
  • Historical reasons (Intel dominance)

Big-endian is ofen considered more “human readable” in memory dumps.

📌 Key Takeaways

  • Endianness defines byte order in memory.
  • Big-endian → MSB first.
  • Little-endian → LSB first.
  • Networking uses big-endian.
  • Most modern PCs use little-endian.
  • Always define byte order when working with binary data.

🏁 Final Thoughts

Endianness is one of those concepts that seems small — until it breaks your system.

If you work with:

  • Networking
  • Embedded systems
  • File parsers
  • Reverse engineering
  • Systems programming

Understanding endianness isn’t optional — it’s essential.

Once you see it in action, it becomes intuitive.


메타데이터
post_id
decd50f5b13c
slug
understanding-endianness-in-computing-with-detailed-examples-decd50f5b13c
url
https://medium.com/@ojha.aniket2002/understanding-endianness-in-computing-with-detailed-examples-decd50f5b13c
canonical_url
https://medium.com/@ojha.aniket2002/understanding-endianness-in-computing-with-detailed-examples-decd50f5b13c
author_url
https://medium.com/@ojha.aniket2002
status
ok
fetched_at
2026-07-08 11:54:00