DEV Community

hassaan-syed
hassaan-syed

Posted on

Why Your Computer Reads Numbers Backwards: Byte Order Explained

What is Byte Order?

Before understanding byte order, we need to understand one thing:

A byte = 8 bits

Many data types use multiple bytes.

For example:

Data Type   Size
char           1 byte
short          2 bytes
int            4 bytes
long long      8 bytes
Enter fullscreen mode Exit fullscreen mode

When a number takes multiple bytes, the computer has to decide:

“In what order should I store these bytes in memory?”

That storage order is called:

Byte Order (Endianness)
1.little order endian
2.big order endian

Most people assume memory stores it like this:

12 34 56 78

But here comes the surprise…

On many computers, it is actually stored like this:

78 56 34 12

Wait… what?

Did the computer reverse the number?

No. Your computer is not confused — this behavior is called Byte Order, also known as Endianness.

In this article, we’ll understand byte order from scratch using diagrams, memory examples, and a practical C program.

*Understanding With an Example
*

Let’s take this hexadecimal number:

0x12345678

This number contains 4 bytes:

12 34 56 78

Now imagine memory locations like this:

Address → 1000 1001 1002 1003

The question is:

Which byte should be placed first?

This is where endianness comes in.

Type 1: Little Endian

In Little Endian, the smallest byte (Least Significant Byte) comes first.

Memory stores:


Address →   1000   1001   1002   1003
Data    →    78      56      34      12
Enter fullscreen mode Exit fullscreen mode

Here:

78 is stored first
12 is stored last
Enter fullscreen mode Exit fullscreen mode

This may look backward, but internally it helps processors perform arithmetic efficiently.

Why is it called “Little” Endian?

Because the little end (least significant byte) is stored first.

Type 2: Big Endian

In Big Endian, the largest byte (Most Significant Byte) comes first.

Memory stores:


Address →   1000   1001   1002   1003
Data    →    12      34      56      78
Enter fullscreen mode Exit fullscreen mode

This looks more natural because humans usually read numbers from left to right.

Why is it called “Big” Endian?

Because the big end (most significant byte) comes first.

Visual Comparison
Number = 0x12345678


Little Endian
┌───────┬───────┬───────┬───────┐
│  78   │  56   │  34   │  12   │
└───────┴───────┴───────┴───────┘
 1000    1001    1002    1003


Big Endian
┌───────┬───────┬───────┬───────┐
│  12   │  34   │  56   │  78   │
└───────┴───────┴───────┴───────┘
 1000    1001    1002    1003
Enter fullscreen mode Exit fullscreen mode

Top comments (0)