Endianness tells us how computers store numbers in memory. It helps us understand how data is arranged when we read or write it.
Endianness (big-endian vs little-endian) in Embedded C
/* No direct syntax in C, but we check endianness like this: */ #include <stdio.h> int main() { unsigned int x = 1; char *c = (char*)&x; if (*c) { // little-endian } else { // big-endian } return 0; }
Endianness is about byte order in memory, not a language keyword.
We use pointers to check how bytes are stored.
unsigned int x = 0x12345678; char *c = (char*)&x; printf("First byte: 0x%x\n", (unsigned char)*c);
unsigned int x = 1; char *c = (char*)&x; if (*c == 1) { printf("Little-endian\n"); } else { printf("Big-endian\n"); }
This program shows how the bytes of a number are stored in memory. It prints each byte and then tells if the system is little-endian or big-endian.
#include <stdio.h> int main() { unsigned int x = 0x12345678; char *c = (char*)&x; printf("Memory bytes of 0x12345678:\n"); for (int i = 0; i < sizeof(x); i++) { printf("Byte %d: 0x%x\n", i, (unsigned char)c[i]); } if (*c == 0x78) { printf("System is little-endian\n"); } else if (*c == 0x12) { printf("System is big-endian\n"); } else { printf("Unknown endianness\n"); } return 0; }
Most modern PCs use little-endian format.
Network protocols often use big-endian (called network byte order).
Always check endianness when sharing binary data between different systems.
Endianness is about the order of bytes in memory for numbers.
Little-endian stores the smallest byte first; big-endian stores the largest byte first.
Knowing endianness helps when working with low-level data or different devices.
Practice
little-endian mean in terms of byte order?Solution
Step 1: Understand byte order concept
Little-endian means the smallest byte (least significant) is stored at the lowest memory address.Step 2: Compare with big-endian
Big-endian stores the largest byte first, so little-endian is the opposite.Final Answer:
The smallest byte is stored first in memory -> Option DQuick Check:
Little-endian = smallest byte first [OK]
- Confusing little-endian with big-endian
- Thinking bytes are stored randomly
- Mixing up byte significance order
Solution
Step 1: Understand the test logic
Setting int x=1 stores 0x00000001 in memory. In little-endian, lowest byte is 1.Step 2: Check pointer dereference
Pointer to char reads first byte. If *c == 1, system is little-endian.Final Answer:
int x = 1; char *c = (char*)&x; if (*c == 1) printf("Little-endian\n"); -> Option AQuick Check:
Check first byte == 1 means little-endian [OK]
- Using wrong value for x
- Checking *c == 0 instead of 1
- Confusing pointer types
0x12345678 stored in memory on a little-endian system, what is the byte order in memory (from lowest to highest address)?Solution
Step 1: Understand little-endian byte order
Little-endian stores the least significant byte first. For 0x12345678, bytes are 12, 34, 56, 78.Step 2: Arrange bytes in memory order
Memory order is 78 (lowest address), 56, 34, 12 (highest address).Final Answer:
78 56 34 12 -> Option BQuick Check:
Little-endian reverses byte order [OK]
- Reading bytes in normal hex order
- Mixing big-endian and little-endian
- Confusing byte significance
void print_big_endian_bytes(int x) {
unsigned char *p = (unsigned char*)&x;
for (int i = 0; i < 4; i++) {
printf("%02x ", p[i]);
}
printf("\n");
}Solution
Step 1: Analyze byte printing logic
The code prints bytes from lowest to highest address, which is system native order.Step 2: Identify missing conversion
To print big-endian, bytes must be printed from highest to lowest address on little-endian systems.Final Answer:
It prints bytes in system's native order, not big-endian -> Option AQuick Check:
Printing p[i] from 0 to 3 prints native order [OK]
- Assuming pointer type causes bug
- Ignoring byte order differences
- Thinking printf format is wrong
0x01 and 0x02. Your little-endian system reads these bytes into memory in order. How do you correctly reconstruct the original number in C?Solution
Step 1: Understand byte order from big-endian device
Big-endian sends most significant byte first: 0x01 then 0x02.Step 2: Combine bytes correctly on little-endian system
Shift first byte left 8 bits and OR with second byte: (0x01 << 8) | 0x02 = 0x0102.Final Answer:
int num = (bytes[0] << 8) | bytes[1]; -> Option CQuick Check:
Shift MSB left 8 bits, OR with LSB [OK]
- Swapping byte order incorrectly
- Adding bytes instead of bitwise OR
- Ignoring device byte order
Ready to prove it?
Pass the test and this pattern moves unknown
