Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Store More than 32 1-bit-flags in a Single 'scalar'

0.00/5 (No votes)
14 Jul 2001 3  
A small class to store more than 32 1-bit flags in a simple class instead of using a DWORD with a maximum of 32 bits

Introduction

Have you ever had the problem that you were storing your bit-flags in a WORD and you were reaching the border of 16 bits? Then you may have used a double word instead of a word. But what are you doing on a 32 bit system when you cross the 32 bit border? Then you have to use other ways to store your bits.

One of our programmers reached that point, a few weeks ago. He came to me to ask me for a way to handle that problem. I told him to build a class that provides all standard operations that might be useful to store and request flags.

These operations are normally the OR-, AND-, NOT- and compare-operators of C++. "You won't need any more than that!", I told him. He didn't believe me and discarded some minor weighty flags to make space for the new ones.

Well, I think this is far away from a real solution!

As luck would have it, I got the same problem a few days ago. "That would be a nice chance to implement my suggestion", I thought. And I did so.

So here is the (very small) class, that dynamically stores as many flags as you will ever need. And the biggest advantage is that you won't have to change your code as much as you may have thought.

So what do you have to do exactly?

  • The first step is the most expensive one: you need to change all your UINT-, WORD- and DWORD types to the type of the new class called Flags.

    Before:

    void foo( unsigned int flags );

    After:

    void foo( Flags flags );

    For performance (Flags isn't a C++-scalar), you might use a const reference (if you can):

    void foo( const Flags& flags );
  • The next step is to change the way you define your flags:

    Before, you may have used code to define a flag that uses the lowest bit:

    #define MYFLAG   0x00000001L

    Some of you might have experienced, that it can be very hard to manage all the bits given as hexadecimal values. Now it will be a little bit easier:

    #define MYFLAG   Flags(0)

    ...sets the lowest bit (bit 0). You soon will notice that this is a much easier way to define your flags.

So that's all! If you forbear from using bit shifting to managing your flags and relying on the Flags class, you may have no problems. And the best: you never have to be afraid to reach a border of maximum number of flags except your RAM's capabilities.

I have also added a small trace method called debug(). It uses the MFC's trace mechanism (if available) and can be ported very easily.

Hope you will have fun with this small class completely, implemented in a single header as inline code.

License

This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.

A list of licenses authors might use can be found here.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here