0

I've recently read:

C/C++ bitfields versus bitwise operators to single out bits, which is faster, better, more portable?

and found multiple respondents making one of the following claims:

Portability

  • Using bit-fields results is not portable.
  • Famous portable software (e.g. OpenBSD) avoids bit-fields for portability reasons.
  • Compilers are incompatible with each other w.r.t. bit-field implementation.
  • "People" didn't like bit-fields since they were introduced.

Public adoption

  • "People" use bitwise operators instead.
  • Developers typically not understand C bit-field code.

I don't understand why that is, especially the first part. Aren't bit-fields part of the C language, since forever? Are they not supported by all standard-compliant C compiler, and thus produce valid, reliable programs when used?

einpoklum
  • 102,731
  • 48
  • 279
  • 553
  • They're supported and standard in a sense that you can access them by foo.bar. The problem is that underlying memory/peripheral bit order and position is not defined by standard, so it's makes it useless when dealing with registers or when transmitting/storing data. – domen Mar 02 '22 at 13:32
  • @domen: So, their problematic _only_ if you were hoping to rely on the mapping of logical to physical bits? And otherwise there's no issue with them? – einpoklum Mar 02 '22 at 15:43
  • Other than that there might be performance issues or suboptimal memory space allocation (I think worst case is int or long per bit). Besides readability there's no guaranteed benefit. I do find it quite annoying that the mappings are not well defined. It would make a lot of embedded code much nicer to read. – domen Mar 03 '22 at 16:05

0 Answers0