DescriptionQ437
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 interpretationInput
bytes of uint32_t value 1 are 01 00 00 00Output
LITTLEExplanation
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.
