Embedded Systems & Firmware

When representing peripheral registers with C bit-field structs, what needs care?

  1. ABit fields always make programs twice as fast
  2. BBit ordering and similar details of bit fields are implementation-defined, so another compiler may manipulate unintended bits
  3. 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.

Related calculators

Books on Embedded Systems & Firmware Ad

Learn hands-on: parts and kits

Building real circuits is a good way to check what you learned in the questions.

As an Amazon Associate I earn from qualifying purchases.

More Embedded Systems & Firmware questions

  1. On many microcontrollers, what is the advantage of declaring a large table that never changes (such as font data) as const?
  2. What can be done through debug interfaces such as JTAG and SWD?
  3. You want to run a control calculation every 1 ms on a microcontroller without an OS. Which implementation is most suitable?
  4. What is the role of a microcontroller's brown-out detector (BOD, brown-out reset)?
  5. On a microcontroller with interrupt priorities, a higher-priority interrupt occurs while a lower-priority handler is running. What generally happens (assuming nesting is allowed)?

Comparisons in this topic