C Programming · Interview question

What are the portability problems with bitfields?

A strong answer

Several layout details are implementation-defined. The bit order, whether the first declared bitfield occupies the least-significant or most-significant bits, depends on the compiler and target ABI, so the same struct can map to opposite bit positions on two toolchains. The standard also doesn't pin down whether a bitfield can straddle the boundary of its underlying storage unit, how padding is inserted, or the effect of the declared type. The consequence: bitfields are unsafe for anything that must match an external bit-for-bit layout across compilers, network/wire protocol headers and datasheet-exact hardware register maps. For those, explicit masks and shifts (reg = (reg & ~MASK) | (val << SHIFT)) are precise and portable. Bitfields are fine for internal data structures compiled by a single known toolchain, where the readability win is real and the layout never crosses a boundary.

What a weak answer sounds like

You know the answer. Do you know what gets you dinged?

Pro breaks down the answer most candidates actually give to this question — and the specific reason an interviewer marks it down. It’s the difference between sounding correct and sounding senior, on all 472 questions.

From the lesson

Structs, Unions & Bitfields

Group related data, overlay the same bytes two ways, and pack flags into bits, plus the padding and alignment rules that decide sizeof and bite packet parsing.

More Structs, Unions & Bitfields questions

Browse all 472 interview questions
What are the portability problems with bitfields? | EmbeddedPrep.io