0
0
Embedded Cprogramming~20 mins

Endianness (big-endian vs little-endian) in Embedded C - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Endianness Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this code snippet?

Consider the following C code that checks the endianness of the system:

unsigned int x = 0x12345678;
char *c = (char*)&x;
printf("%02x %02x %02x %02x", (unsigned char)c[0], (unsigned char)c[1], (unsigned char)c[2], (unsigned char)c[3]);

What will be printed on a little-endian system?

Embedded C
unsigned int x = 0x12345678;
char *c = (char*)&x;
printf("%02x %02x %02x %02x", (unsigned char)c[0], (unsigned char)c[1], (unsigned char)c[2], (unsigned char)c[3]);
A00 00 00 00
B12 34 56 78
C78 56 34 12
D78 34 56 12
Attempts:
2 left
💡 Hint

Think about how bytes are stored in memory on little-endian systems.

Predict Output
intermediate
2:00remaining
What is the output of this code snippet on a big-endian system?

Given the same code as before:

unsigned int x = 0x12345678;
char *c = (char*)&x;
printf("%02x %02x %02x %02x", (unsigned char)c[0], (unsigned char)c[1], (unsigned char)c[2], (unsigned char)c[3]);

What will be printed on a big-endian system?

Embedded C
unsigned int x = 0x12345678;
char *c = (char*)&x;
printf("%02x %02x %02x %02x", (unsigned char)c[0], (unsigned char)c[1], (unsigned char)c[2], (unsigned char)c[3]);
A12 34 56 78
B78 56 34 12
C00 00 00 00
D34 12 78 56
Attempts:
2 left
💡 Hint

Recall how big-endian systems store the most significant byte first.

🧠 Conceptual
advanced
1:30remaining
Which statement correctly describes endianness?

Choose the correct statement about endianness in computer systems.

ALittle-endian stores the least significant byte at the lowest memory address.
BBig-endian stores the least significant byte at the highest memory address.
CLittle-endian stores the most significant byte at the lowest memory address.
DEndianness only affects how data is displayed on the screen.
Attempts:
2 left
💡 Hint

Think about which byte is stored first in memory for little-endian systems.

🔧 Debug
advanced
2:00remaining
What error occurs when this code runs on a little-endian system?

Consider this code snippet that tries to convert a 4-byte array to an integer:

unsigned char bytes[4] = {0x12, 0x34, 0x56, 0x78};
unsigned int *p = (unsigned int*)bytes;
printf("%x", *p);

What problem might occur on a little-endian system?

AThe printed value will be 0x12345678, which matches the byte order.
BThe printed value will be 0x78563412, which is incorrect for the intended value.
CThe code causes a segmentation fault due to invalid pointer casting.
DThe code causes a compile-time error due to incompatible types.
Attempts:
2 left
💡 Hint

Think about how the bytes are interpreted when cast to an integer pointer on little-endian.

🚀 Application
expert
3:00remaining
How to write a portable function to swap endianness of a 32-bit integer?

Which of the following C functions correctly swaps the byte order of a 32-bit unsigned integer (converts big-endian to little-endian or vice versa)?

A
unsigned int swap_endian(unsigned int val) {
  return (val << 24) | (val << 8) | (val >> 8) | (val >> 24);
}
B
unsigned int swap_endian(unsigned int val) {
  return (val & 0xFF) << 24 | (val & 0xFF00) << 8 | (val & 0xFF0000) >> 8 | (val & 0xFF000000) >> 24;
}
C
unsigned int swap_endian(unsigned int val) {
  return val ^ 0xFFFFFFFF;
}
D
unsigned int swap_endian(unsigned int val) {
  return ((val >> 24) & 0x000000FF) |
         ((val >> 8) & 0x0000FF00) |
         ((val << 8) & 0x00FF0000) |
         ((val << 24) & 0xFF000000);
}
Attempts:
2 left
💡 Hint

Think about shifting and masking each byte to its new position.