Skip to question
asic.fyi
DesignVerificationSystemVerilogFirmwareArchitectureInterviews
asic.fyi/Interview questions/Detect machine endianness in C

Hardware interview practice

Detect machine endianness in C

EasyHardware-Software IntegrationC

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

Try it in the question bankReason first. Then compare.

Keep this exact question selected while you check your answer and review the full solution.

Practice this question →
Starting point

Question code

enum endian { LITTLE, BIG, UNKNOWN };
enum endian detect_endian(void);
Reviewed example

Work through one case

Input
bytes of uint32_t value 1 are 01 00 00 00
Expected output
LITTLE

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

What to cover

Requirements

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

Related questions

Hardware-Software IntegrationLittle-endian byte order→
asic.fyi · Learn silicon end to end.info@asic.fyi