Question

I am working with C and C++ for some time.

While learning the basics you can bump into such interesting thing as bit fields. Usage of bit fields in programming practice has somehow controversial character.

In which kind of situations this low-level feature usage provides a real benefit, and are there concrete examples of using bit fields properly?

Was it helpful?

Solution 2

There are several use cases for bit fields, even on modern machines.

The first would be when you are handling register level logic. This is common when you are setting modes and how certain pieces of hardware work. This is even more common on embedded devices. On Arduino devices, for example, the "PinMode" logic is basically setting individual bits high or low to indicate whether a digital I/O pin is in "input" or "output" mode.

http://arduino.cc/en/Reference/pinMode

Secondly, when writing optimized, in-line assembly code in a C/C++ program. There are times where you want to take advantage of hardware-optimized instructions to speed up your program's execution as much as possible:

http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html

A final common example is when writing packet drivers or implementing specific protocols. I recently just posted a question on this, where it turns out I was using a 32-bit variable instead of an 8-bit variable composed of bitfields, which was causing my code to break:

Basic NTP Client in Windows in Visual C++

So, in short: when talking directly to hardware, or in networking code.

OTHER TIPS

When working with embedded systems and microcontrollers, individual bits in a register may be associated with a processor setting or input/output. Using bit fields allows these individual bits to be worked with by name instead of doing bitwise operations on the entire register.

It's mostly an aesthetic feature but can increase code readability in some applications.

There's probably not much use for bit fields on a modern, high performance machine, but for smaller machines, they can be very useful to save memory, if you have large arrays of the structures. Other than saving memory, however, there's no use for them.

In addition to the other answers, in some scenarios, using bit fields can improve both memory usage and performance.

Save memory by packing together properties which need few bits to express the range of possible values. Why put 8 bool properties as 8 bool members, when a single byte gives to the ability to store 8 boolean values in each bit - instead of 8 bytes you use only 1, 7 bytes saved is quite significant. Naturally, you would typically use a 32, 64 bit or wider bitfields. I have a similar scenario with a lots of objects with lots of properties which can be expressed in one or few bits, and for cases with high object count (millions) the memory savings are indeed significant.

Increase performance - although bitfields come with a small performance penalty (to access the actual value with shifting and masking) those operations are really fast. Packing more data and wasting less bits can give you better cache efficiency, which can result in performance gain that is greater than the bit fields access penalty. Not only it may be faster to access individual bits than fetching another line from the cache and more probable to find the data in the cache if it is packed, but this will pollute the cache less, leaving more available space for other processes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top