What is endianness, and how do you deal with it when exchanging data between devices?
What the interviewer wants to hear
- Order in which bytes of a multi-byte value are stored
- Little-endian stores least significant byte first; big-endian most significant first
- Most MCUs are little-endian; network byte order is big-endian
- Define the byte order in the protocol and convert explicitly with shifts
- Avoid sending raw structs because of padding and byte order
Model answer
Endianness is the order in which a multi-byte value is stored in memory. Little-endian puts the least significant byte at the lowest address, and big-endian puts the most significant byte first. Most Cortex-M and RISC-V MCUs are little-endian, while many network protocols and some sensors use big-endian, often called network byte order. When devices exchange data, I make the byte order part of the protocol specification and convert explicitly, building values with shifts, such as (b[0] << 8) | b[1], instead of casting a byte pointer to a wider type. That code works on any CPU and also avoids alignment problems. I avoid sending raw C structs because padding and byte order differ between compilers and processors, and I write unit tests with known byte sequences.
Can you answer it under time pressure?
Take a timed easy round and get scored on every answer.
More easy Embedded Systems Engineer questions
- What is the difference between a microcontroller and a microprocessor?
- What is an interrupt, and how is it different from polling?
- Compare UART, SPI and I2C. When would you use each?
- Explain volatile and const in embedded C. Can a variable be both?
- When would you use bare-metal firmware versus an RTOS?
- What GPIO modes does a typical microcontroller pin support, and when would you use each?
- What is PWM, how is it generated on a microcontroller, and what is it used for?
- What is a bootloader in an embedded device, and why would a product need one?
- How do you approach reading a datasheet for a new sensor or microcontroller?