0
0
Embedded Cprogramming~10 mins

Endianness (big-endian vs little-endian) in Embedded C - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Concept Flow - Endianness (big-endian vs little-endian)
Start with multi-byte data
Check system endianness
Big-endian: store MSB first
Little-endian: store LSB first
Memory stores bytes in order
Data read back accordingly
This flow shows how a system decides to store multi-byte data in memory either with the most significant byte first (big-endian) or least significant byte first (little-endian).
Execution Sample
Embedded C
unsigned int x = 0x12345678;
unsigned char *p = (unsigned char *)&x;
printf("Byte 0: %02x\n", p[0]);
printf("Byte 1: %02x\n", p[1]);
This code stores a 4-byte integer and prints the first two bytes in memory to show how bytes are ordered depending on endianness.
Execution Table
StepActionMemory Byte IndexByte Value (hex)Explanation
1Store 0x12345678 in variable x--Variable x holds 4 bytes: 12 34 56 78
2Pointer p points to x's first byte--p[0] points to first byte in memory for x
3Read p[0]078 (little-endian) or 12 (big-endian)First byte in memory depends on endianness
4Read p[1]156 (little-endian) or 34 (big-endian)Second byte in memory depends on endianness
5Print bytes--Output shows byte order in memory
6End--Program ends after printing bytes
💡 All bytes printed; program ends after showing memory byte order.
Variable Tracker
VariableInitialAfter Step 1After Step 2After Step 5Final
xundefined0x123456780x123456780x123456780x12345678
pundefinedundefinedaddress of xaddress of xaddress of x
p[0]undefinedundefinedfirst byte of x in memoryprinted valueprinted value
p[1]undefinedundefinedsecond byte of x in memoryprinted valueprinted value
Key Moments - 3 Insights
Why does p[0] print 0x78 on little-endian but 0x12 on big-endian?
Because little-endian stores the least significant byte (0x78) at the lowest memory address, which p[0] points to. Big-endian stores the most significant byte (0x12) first, so p[0] points there. See execution_table rows 3 and 4.
Is the value of x changed by endianness?
No, x holds the same number 0x12345678 in both cases. Only the order of bytes in memory differs. This is shown in variable_tracker where x remains constant.
Why do we use a char pointer to inspect bytes?
Because a char pointer accesses memory one byte at a time, letting us see the exact byte order stored. This is shown in execution_table step 2 and 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of p[0] on a little-endian system at step 3?
A0x12
B0x78
C0x34
D0x56
💡 Hint
Check execution_table row 3 under 'Byte Value (hex)' for little-endian.
At which step does the program print the bytes stored in memory?
AStep 2
BStep 4
CStep 5
DStep 6
💡 Hint
Look for the step mentioning 'Print bytes' in execution_table.
If we changed x to 0xAABBCCDD, what would p[1] be on a big-endian system at step 4?
A0xBB
B0xCC
C0xAA
D0xDD
💡 Hint
In big-endian, bytes are stored MSB first: AA BB CC DD. p[1] points to second byte.
Concept Snapshot
Endianness decides byte order in memory for multi-byte data.
Big-endian stores the most significant byte first.
Little-endian stores the least significant byte first.
Use a char pointer to inspect byte order.
Understanding endianness helps with data exchange and debugging.
Full Transcript
This lesson shows how computers store multi-byte numbers differently depending on endianness. We use a 4-byte integer 0x12345678 and a char pointer to look at each byte in memory. On little-endian systems, the least significant byte 0x78 is stored first, so p[0] reads 0x78. On big-endian systems, the most significant byte 0x12 is stored first, so p[0] reads 0x12. The variable x itself holds the same number in both cases; only the byte order in memory changes. This is important to know when working with low-level data or communicating between different systems.