2

Possible Duplicate:
When is it worthwhile to use bit fields?

I was looking up bitwise operators recently and stumbled upon the concept of the bitfield. It seems interesting and is a very cool concept, but when and/or why would a person use this in their code?

I know it's used quite a bit in embedded systems programming, but why (I can't seem to find anything about why its useful)? Are there any advantages to it? And where are some other places bitfields are useful?

Community
  • 1
  • 1
Rivasa
  • 6,510
  • 3
  • 35
  • 64

3 Answers3

3

In general, use bitfields when you don't care about speed and you don't care about memory layout. IF you care about these things, then don't use bitfields.

If you have a set of boolean flags, then you can pack them using bitfields (reducing size needed to store). However, only use the bitfield to access the bitfield.

It is the classic size vs. speed problem.

An additional caveat is that if you have a set of bitfields that are smaller than the native word, then your compiler will probably try to pad and align the bitfield struct. So you have to end up #pragma pack'ing the struct or use at least a native word. So if you are on a 32 bit machine and you happen to have 32 boolean flags that are only used internally, then this would be a good use of bitfields.

Josh Petitt
  • 9,371
  • 12
  • 56
  • 104
2

Some uses that immediately come to mind are:

  • implementing communications protocols;
  • storing user data in objects where you have limited space;
  • extending data structures in existing protocols (similar to the above);
  • performing multiple tests in a single operation;
paddy
  • 60,864
  • 6
  • 61
  • 103
  • 2
    It's often not a good idea to use bitfields when implementing communications protocols, because the C standard does not define exactly how the bits are arranged in memory (it might be left to right, or right to left, or something else entirely). Only the compiler that is actually compiling the code knows for certain. – Greg Hewgill Sep 26 '12 at 01:47
  • 1
    @GregHewgill When you know how your compiler handles bitfields, it's not a problem. The code loses portability, true. – Alexey Frunze Sep 26 '12 at 01:52
  • @paddy, can you elaborate on the communications protocols part? – Rivasa Sep 26 '12 at 01:59
  • 1
    @GregHewgill When you're communicating between similar architectures there's no problem. If there is a translation problem between architectures then it's handled by defining the bits differently when you compile for them. If you really need to keep your footprint small and have a lot of flags, this is preferable. – paddy Sep 26 '12 at 02:04
  • 3
    Two different compilers for the same architecture can choose to lay out the bitfields differently. The order is not specified by the C standard, and has nothing to do with endianness. – Greg Hewgill Sep 26 '12 at 02:06
  • @Link The reason I suggested communications is that often you are trying to keep packet sizes to a minimum. There are many instances where you need to stamp packets with control flags. A real-world example of flags in communications is (TCP)[http://en.wikipedia.org/wiki/Transmission_Control_Protocol]. – paddy Sep 26 '12 at 02:12
  • @GregHewgill Oh, I may have missed the point. For some reason I had decided this question was really about the packing of bits, rather than the built-in bitfields. Personally, I don't use those! – paddy Sep 26 '12 at 02:17
  • @paddy, in general don't put bitfields on the wire (i.e. don't try to serialize them). Yes, if you know what your compiler is going to do, then you can. But its often more hassle than its worth and you are writing landmine code when someone switches compilers. – Josh Petitt Sep 26 '12 at 02:45
  • @paddy, IMHO macros are the preferred solution for manipulating flags in communication packets. – Josh Petitt Sep 26 '12 at 02:52
2

I have used bitfields as part of unions to encompass register in embedded system i.e. control registers of microcontrollers, codecs. They are very useful in depicting physical layout of registers as software constructs thereby conveying readability. They were commonly used in for device driver implementations. A few years back 8-bit micros with very little flash and ram memory were common and therefore bitfields were common. These days 32-bit micros with lots of ram/flash means that bitfields are not necessary.

bobestm
  • 1,334
  • 1
  • 9
  • 8
  • I've found that it is risky to use bitfields for physical layout of registers. If you trust your compiler and you know what it will do, then it might be okay. Some compilers (IAR) even use bitfields to access bits in registers. However, you are treading into "implementation defined" behavior and it can bite you. – Josh Petitt Sep 26 '12 at 02:50
  • I agree with your comments. I was only reflecting on past implementations. Things have changed since then. – bobestm Sep 26 '12 at 04:08