Skip to the selected question
ASIC.FYI

ASIC Question Bank

1,000+ hardware interview questions
DescriptionQ437
Page ↗
Q437FWAppleASIC interview problem

Detect machine endianness in C

TechniquesFWCEndiannessObject representation
DifficultyEasy
TopicHardware-Software Integration
LanguageC
Requirements4 checkpoints
01

Problem

Implement detect_endian() without undefined behavior. Inspect the stored bytes of a multi-byte unsigned integer whose value is 1.

Starting declarationC
enum endian { LITTLE, BIG, UNKNOWN };
enum endian detect_endian(void);

Example input and output

Use this case to check your interpretation
Input
bytes of uint32_t value 1 are 01 00 00 00
Output
LITTLE
Explanation

The least-significant byte is stored at the lowest address.

02

Requirements (4)

  • Inspect object representation only through an unsigned char pointer.
  • Return LITTLE when the first byte is 1 and BIG when the last byte is 1.
  • Return UNKNOWN for any other representation.
  • Do not infer memory byte order by shifting the numeric value.