Bird
Raised Fist0
Embedded Cprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does little-endian mean in terms of byte order?
easy
A. Bytes are stored in reverse alphabetical order
B. The largest byte is stored first in memory
C. Bytes are stored in random order
D. The smallest byte is stored first in memory

Solution

  1. Step 1: Understand byte order concept

    Little-endian means the smallest byte (least significant) is stored at the lowest memory address.
  2. Step 2: Compare with big-endian

    Big-endian stores the largest byte first, so little-endian is the opposite.
  3. Final Answer:

    The smallest byte is stored first in memory -> Option D
  4. Quick Check:

    Little-endian = smallest byte first [OK]
Hint: Little-endian starts with smallest byte first [OK]
Common Mistakes:
  • Confusing little-endian with big-endian
  • Thinking bytes are stored randomly
  • Mixing up byte significance order
2. Which of the following C code snippets correctly checks if the system is little-endian?
easy
A. int x = 1; char *c = (char*)&x; if (*c == 1) printf("Little-endian\n");
B. int x = 1; char *c = (char*)&x; if (*c == 0) printf("Little-endian\n");
C. int x = 256; char *c = (char*)&x; if (*c == 1) printf("Little-endian\n");
D. int x = 256; char *c = (char*)&x; if (*c == 0) printf("Little-endian\n");

Solution

  1. Step 1: Understand the test logic

    Setting int x=1 stores 0x00000001 in memory. In little-endian, lowest byte is 1.
  2. Step 2: Check pointer dereference

    Pointer to char reads first byte. If *c == 1, system is little-endian.
  3. Final Answer:

    int x = 1; char *c = (char*)&x; if (*c == 1) printf("Little-endian\n"); -> Option A
  4. Quick Check:

    Check first byte == 1 means little-endian [OK]
Hint: Check first byte of int 1 for 1 to detect little-endian [OK]
Common Mistakes:
  • Using wrong value for x
  • Checking *c == 0 instead of 1
  • Confusing pointer types
3. Given the 32-bit hex number 0x12345678 stored in memory on a little-endian system, what is the byte order in memory (from lowest to highest address)?
medium
A. 12 34 56 78
B. 78 56 34 12
C. 34 12 78 56
D. 56 78 12 34

Solution

  1. Step 1: Understand little-endian byte order

    Little-endian stores the least significant byte first. For 0x12345678, bytes are 12, 34, 56, 78.
  2. Step 2: Arrange bytes in memory order

    Memory order is 78 (lowest address), 56, 34, 12 (highest address).
  3. Final Answer:

    78 56 34 12 -> Option B
  4. Quick Check:

    Little-endian reverses byte order [OK]
Hint: Little-endian reverses byte order from hex [OK]
Common Mistakes:
  • Reading bytes in normal hex order
  • Mixing big-endian and little-endian
  • Confusing byte significance
4. The following code attempts to print the bytes of an integer in big-endian order on any system. What is the bug?
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");
}
medium
A. It prints bytes in system's native order, not big-endian
B. Pointer type is incorrect, should be int*
C. Loop runs too many times causing overflow
D. printf format specifier is wrong for bytes

Solution

  1. Step 1: Analyze byte printing logic

    The code prints bytes from lowest to highest address, which is system native order.
  2. Step 2: Identify missing conversion

    To print big-endian, bytes must be printed from highest to lowest address on little-endian systems.
  3. Final Answer:

    It prints bytes in system's native order, not big-endian -> Option A
  4. Quick Check:

    Printing p[i] from 0 to 3 prints native order [OK]
Hint: Print bytes reversed for big-endian on little-endian [OK]
Common Mistakes:
  • Assuming pointer type causes bug
  • Ignoring byte order differences
  • Thinking printf format is wrong
5. You receive a 16-bit number from a big-endian device as two bytes: 0x01 and 0x02. Your little-endian system reads these bytes into memory in order. How do you correctly reconstruct the original number in C?
hard
A. int num = bytes[0] + bytes[1];
B. int num = (bytes[1] << 8) | bytes[0];
C. int num = (bytes[0] << 8) | bytes[1];
D. int num = bytes[1] * 256 + bytes[0];

Solution

  1. Step 1: Understand byte order from big-endian device

    Big-endian sends most significant byte first: 0x01 then 0x02.
  2. Step 2: Combine bytes correctly on little-endian system

    Shift first byte left 8 bits and OR with second byte: (0x01 << 8) | 0x02 = 0x0102.
  3. Final Answer:

    int num = (bytes[0] << 8) | bytes[1]; -> Option C
  4. Quick Check:

    Shift MSB left 8 bits, OR with LSB [OK]
Hint: Shift first byte left 8 bits, OR with second byte [OK]
Common Mistakes:
  • Swapping byte order incorrectly
  • Adding bytes instead of bitwise OR
  • Ignoring device byte order
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard