Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there reasons to avoid bit-field structure members?

Tags:

I long knew there are bit-fields in C and occasionally I use them for defining densely packed structs:

typedef struct Message_s {      unsigned int flag : 1;      unsigned int channel : 4;      unsigned int signal : 11; } Message; 

When I read open source code, I instead often find bit-masks and bit-shifting operations to store and retrieve such information in hand-rolled bit-fields. This is so common that I do not think the authors were not aware of the bit-field syntax, so I wonder if there are reasons to roll bit-fields via bit-masks and shifting operations your own instead of relying on the compiler to generate code for getting and setting such bit-fields.

like image 694
wirrbel Avatar asked Sep 03 '17 08:09

wirrbel


People also ask

What are the important points to be considered when implementing bit fields in structures?

Bit fields can be used to reduce memory consumption when a program requires a number of integer variables which always will have low values. For example, in many systems storing an integer value requires two bytes (16-bits) of memory; sometimes the values to be stored actually need only one or two bits.

Are Bitfields useful?

A bit-field is used to club together many variables into one object, similar to a structure. This allows for reduced memory usage and is especially useful in an embedded environment.

What is a bit-field Why are bit fields used with structures?

These space-saving structure members are called bit fields, and their width in bits can be explicitly declared. Bit fields are used in programs that must force a data structure to correspond to a fixed hardware representation and are unlikely to be portable.


2 Answers

Why other programmers use hand-coded bit manipulations instead of bitfields to pack multiple fields into a single word?

This answer is opinion based as the question is quite open:

  • Many programmers are unaware of the availability of bitfields or unsure about their portability and precise semantics. Some even distrust the compiler's ability to produce correct code. They prefer to write explicit code that they understand.

    As commented by Cornstalks, this attitude is rooted in real life experience as explained in this article.

  • Bitfield's actual memory layout is implementation defined: if the memory layout must follow a precise specification, bitfields should not be used and hand-coded bit manipulations may be required.

  • The handing of signed values in signed typed bitfields is implementation defined. If signed values are packed into a range of bits, it may be more reliable to hand-code the access functions.

like image 111
chqrlie Avatar answered Oct 04 '22 10:10

chqrlie


Are there reasons to avoid bitfield-structs?

bitfield-structs come with some limitations:

  1. Bit fields result in non-portable code. Also, the bit field length has a high dependency on word size.
  2. Reading (using scanf()) and using pointers on bit fields is not possible due to non-addressability.
  3. Bit fields are used to pack more variables into a smaller data space, but cause the compiler to generate additional code to manipulate these variables. This results in an increase in both space as well as time complexities.
  4. The sizeof() operator cannot be applied to the bit fields, since sizeof() yields the result in bytes and not in bits.

Source

So whether you should use them or not depends. Read more in Why bit endianness is an issue in bitfields?


PS: When to use bit-fields in C?

like image 38
gsamaras Avatar answered Oct 04 '22 10:10

gsamaras