Embedded Systems & Firmware
When representing peripheral registers with C bit-field structs, what needs care?
- ABit fields always make programs twice as fast
- BBit ordering and similar details of bit fields are implementation-defined, so another compiler may manipulate unintended bits
- CBit fields cannot represent values of 8 bits or more
Show answer and explanation
Answer: B. Bit ordering and similar details of bit fields are implementation-defined, so another compiler may manipulate unintended bits
Bit fields read nicely, but the C standard leaves to the implementation whether bits are allocated from the top or bottom of a unit, how fields crossing boundaries are handled and the access width. Code that works with one compiler may therefore read or write different bits with another compiler or CPU. Headers supplied by microcontroller vendors are usually written with masks and shifts.
- A. Speed does not necessarily improve.
- B. Correct. Portability needs attention.
- C. Fields of 8 or more bits are possible.