🚀 Learn BLE on Linux (BlueZ) – Programming Part 1
Hands-on Bluetooth Low Energy course. Special price: ₹399
Enroll Now on Udemy →Little vs Big Endian
Lecture 47 of our free C programming course: understand byte order, memory layout and why it matters in embedded C programming.
Chapter 10
Structure and Union
Level
Beginner
Reading time
10 minutes
Every multi-byte number in your program, such as a 16-bit sensor reading or a 32-bit timer value, has to be laid out in memory one byte at a time. Little vs big endian is the rule that decides which byte goes first. This lecture of our free C tutorial explains byte order from scratch, shows exactly how a value looks in RAM on each kind of machine, and connects it to the situations where embedded C programming forces you to care: network packets, wireless protocols, file formats and talking to chips with a different byte order than your CPU.
What You Will Learn
- What a most significant byte (MSB) and least significant byte (LSB) are
- How little-endian and big-endian machines store the same number
- Which processors and protocols use which byte order
- How to print the real byte layout of a variable in C
- How to convert byte order safely with standard functions
Prerequisites
You need pointers to char, hexadecimal numbers and fixed-width integer types such as uint32_t. If hex still feels slow, revisit the number systems chapter of this free embedded C course first.
MSB, LSB and Memory Addresses
Take the hexadecimal number 0x11223344. It needs four bytes. Reading from the left, 0x11 carries the largest weight, so it is the most significant byte. The rightmost byte 0x44 carries the smallest weight and is the least significant byte.
Memory, however, is a row of numbered boxes. The question is simply: which box gets which byte? Two answers exist, and both are valid designs.
| Byte order | Rule | 0x11223344 at addresses 0..3 |
|---|---|---|
| Little-endian | LSB stored at the lowest address | 44 33 22 11 |
| Big-endian | MSB stored at the lowest address | 11 22 33 44 |
Memory trick
Little-endian means the “little end” (least significant byte) comes first. Big-endian means the “big end” comes first. The names come from a famous satire about which end of an egg to crack, because the argument over byte order has always been more about convention than correctness.
Little vs Big Endian: Who Uses Which?
| System or protocol | Byte order | Why you may meet it |
|---|---|---|
| x86 and x86-64 PCs | Little-endian | Your laptop, where you test your code |
| ARM Cortex-M microcontrollers | Little-endian in almost every real configuration | STM32, nRF and similar chips |
| RISC-V | Little-endian | Newer open-hardware boards |
| TCP/IP headers (“network byte order”) | Big-endian | Sockets and Ethernet firmware |
| Bluetooth Low Energy attribute values | Little-endian | Sensor and wearable firmware |
| Older Motorola and some PowerPC systems | Big-endian | Legacy industrial boards |
The practical lesson: your own CPU and the thing you talk to may disagree. The moment bytes cross a wire, a file or a shared-memory boundary, byte order becomes part of your design.
Seeing Byte Order in Real C Code
You do not need a special tool to see byte order. A pointer to unsigned char may legally examine the bytes of any object, so you can print each byte in address order. The program below also asks the compiler for the target byte order and shows what the standard network conversion function does.
/* file: endian_probe.c */
#include <stdio.h>
#include <stdint.h>
#include <arpa/inet.h>
int main(void)
{
uint32_t word = 0x11223344u;
const unsigned char *p = (const unsigned char *)&word;
#if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
puts("compiler says: little-endian target");
#elif defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
puts("compiler says: big-endian target");
#endif
for (size_t i = 0; i < sizeof word; i++)
printf("offset %zu -> 0x%02X\n", i, p[i]);
uint32_t net = htonl(word);
p = (const unsigned char *)&net;
printf("after htonl: %02X %02X %02X %02X\n", p[0], p[1], p[2], p[3]);
return 0;
}
$ gcc -std=c11 -Wall -Wextra -o endian_probe endian_probe.c
$ ./endian_probe
compiler says: little-endian target
offset 0 -> 0x44
offset 1 -> 0x33
offset 2 -> 0x22
offset 3 -> 0x11
after htonl: 11 22 33 44
On a little-endian PC the first byte printed is 0x44. After htonl (host to network long) the bytes appear as 11 22 33 44, which is exactly what a TCP/IP header expects. On a big-endian machine, htonl does nothing and the first output block would already read 11 22 33 44.
Little vs Big Endian Conversion in C
| Function | Meaning | Width |
|---|---|---|
| htons / ntohs | host to or from network order | 16 bit |
| htonl / ntohl | host to or from network order | 32 bit |
| htole32 / le32toh | host to or from little-endian (glibc, endian.h) | 32 bit |
| htobe32 / be32toh | host to or from big-endian (glibc, endian.h) | 32 bit |
On bare-metal firmware without a C library you will often write your own conversion with shifts. Shifts operate on values, not on memory, so they work the same on every CPU:
static inline uint32_t read_be32(const uint8_t *buf)
{
return ((uint32_t)buf[0] << 24) |
((uint32_t)buf[1] << 16) |
((uint32_t)buf[2] << 8) |
(uint32_t)buf[3];
}
Why the shift version is portable
It builds the number arithmetically from the byte stream, so the result is right no matter which byte order the CPU uses. Casting the buffer pointer to uint32_t * would break on both byte order and alignment.
Common Little vs Big Endian Mistakes
Casting a receive buffer to a structure. It bakes in your CPU byte order, structure padding and alignment all at once. Parse fields one by one instead.
- Sending an
intover a UART or socket withwriteand expecting the other side to agree on its byte order and size. - Calling
htonstwice, which silently undoes the conversion. - Testing only on your little-endian laptop and never reasoning about the big-endian case.
- Confusing bit order inside a byte with byte order inside a word. They are separate topics.
Best Practices for Little vs Big Endian
- Document the byte order of every protocol field in your header comments.
- Convert at the boundary: swap when data enters or leaves your system, then work in native order inside.
- Use fixed-width types such as
uint16_tanduint32_tfor anything that crosses a wire. - Write a tiny unit test with a known byte sequence so a byte-order bug shows up immediately.
Performance and Security Considerations
Swapping bytes is cheap. Modern ARM Cortex-M cores have single instructions that reverse the bytes of a 16-bit or 32-bit word, and compilers turn htonl or a shift pattern into that instruction automatically. So never avoid correct conversion for speed reasons; the cost is a few cycles per field, while a byte-order bug can cost days of debugging.
Security matters too. Network and wireless parsers read lengths and offsets from untrusted data. If you decode a length field with the wrong byte order, a small number can become a huge one, or the reverse, and your buffer checks will be wrong. Always convert first, then validate the value against the real buffer size, and only then copy data. Many real-world firmware vulnerabilities began as a length field that was trusted before it was decoded properly.
Practice Exercise
Write a program that stores 0xCAFEBABE in a uint32_t and prints its four bytes in memory order. Then call htonl and print again. Predict the output on paper for both a little-endian and a big-endian machine before you run it. If your two predictions match what the program prints on your PC, you have understood byte order.
Summary and Key Takeaways
- Little vs big endian differs only in byte order: little-endian stores the least significant byte at the lowest address; big-endian stores the most significant byte there.
- x86, ARM Cortex-M and RISC-V are little-endian; TCP/IP headers use big-endian network byte order.
- Inspect memory with a
const unsigned char *pointer, and convert withhtonl-style functions or explicit shifts.
Next we will use a union to detect little vs big endian in a single small program, one of the most popular embedded C interview exercises.
Frequently Asked Questions
What is little vs big endian in simple words?
It is the order in which the bytes of a multi-byte number are stored in memory.
Is ARM little-endian or big-endian?
ARM cores support both in principle, but the Cortex-M microcontrollers you will use in embedded work run little-endian.
What is network byte order?
Big-endian. Internet protocol headers define their multi-byte fields most significant byte first.
Does endianness affect single-byte values?
No. A char or uint8_t is one byte, so there is nothing to order.
Does endianness change how arrays are stored?
No. Array elements stay in index order; only the bytes inside each multi-byte element are affected.
Do shifts and bitwise operators depend on endianness?
No. They work on values, which is why shift-based parsing is portable.
Keep Learning Embedded C for Free
Follow the full free embedded C course on EmbeddedPathashala and practise every concept with runnable examples.

2 Comments