Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How slow are bit fields in C++

Tags:

c++

bit-fields

I have a C++ application that includes a number of structures with manually controlled bit fields, something like

#define FLAG1   0x0001  
#define FLAG2   0x0002      
#define FLAG3   0x0004      

class MyClass
{
'
'
  unsigned Flags;

  int IsFlag1Set() { return Flags & FLAG1; }
  void SetFlag1Set() { Flags |= FLAG1; }
  void ResetFlag1() { Flags &= 0xffffffff ^ FLAG1; }
'
'
};

For obvious reasons I'd like to change this to use bit fields, something like

class MyClass
{
'
'
  struct Flags
  {
    unsigned Flag1:1;
    unsigned Flag2:1;
    unsigned Flag3:1;
  };
'
'
};

The one concern I have with making this switch is that I've come across a number of references on this site stating how slow bit fields are in C++. My assumption is that they are still faster than the manual code shown above, but is there any hard reference material covering the speed implications of using bit fields on various platforms, specifically 32bit and 64bit windows. The application deals with huge amounts of data in memory and must be both fast and memory efficient, which could well be why it was written this way in the first place.

like image 434
SmacL Avatar asked Apr 14 '10 14:04

SmacL


People also ask

Should you use bit fields in C?

Bit fields are used when the storage of our program is limited. Need of bit fields in C programming language: Reduces memory consumption. To make our program more efficient and flexible.

How do bit fields work in C?

In programming terminology, a bit field is a data structure that allows the programmer to allocate memory to structures and unions in bits in order to utilize computer memory in an efficient manner. Since structures and unions are user-defined data types in C, the user has an idea of how much memory will they occupy.

Are Bitfields useful?

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.


2 Answers

The two examples should be very similar in speed because the compiler will have to end up issuing pretty much the same instructions for bit-masking in both cases. To know which is really best, run a few simple experiments. But don't be surprised if the results are inconclusive; that's what I'm predicting...

You might be better saying that the bitfields are of type bool though.

like image 137
Donal Fellows Avatar answered Sep 21 '22 11:09

Donal Fellows


General advice for this kind of question: set up a simple program comparing your situation as exactly as you can (operations, hardware, etc.) and measure your performance difference yourself.

For this question on bitfields vs masking, I doubt you'll see significant performance differences - the bitfield code may need a shift or two more than masking, depending on the compiler. Whether that's noticeable in your application or not is something you need to answer. There's a big difference in the considerations for mask-programmable embedded code vs. desktop applications, for example.

like image 40
mpez0 Avatar answered Sep 20 '22 11:09

mpez0