Click here to Skip to main content
15,904,415 members
Articles / Programming Languages / Objective C

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

Rate me:
Please Sign up or sign in to vote.
2.00/5 (1 vote)
14 Jul 20012 min read 98.1K   464   21   12
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:

    C++
    void foo( unsigned int flags );

    After:

    C++
    void foo( Flags flags );

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

    C++
    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:

    C++
    #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:

    C++
    #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.


Written By
Web Developer
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralBigFloat... Pin
5-Jun-02 6:58
suss5-Jun-02 6:58 
GeneralAnother approach Pin
15-May-02 7:16
suss15-May-02 7:16 
In the class definition (.h) define the following flags

int flag1 : 1;
int flag2 : 1;
int flag3 : 1;
int flag4 : 1;
int flag5 : 1;
int flag6 : 1;
int flag7 : 1;
int flag8 : 1;
int flag9 : 1;

More if you want.

In your class constructor (or some other function) compare the address of flag1 and flag2. They should be the same. Then compare the address of flag1 and flag9, you should see a single byte offset. NOTE: You have to use you debugger to compare address. You cannot take the address of the bit-field (&flag1 will generate an error). When you use bit fields, the compiler will not allocate a new int until the previous one is full, e.g. you get 32 flags in one int, the 33rd flag will go into the next int. An exception to this rule is if you define a non bit-field variable in the middle of the list of flags.


Using this type of flag is super easy for the most part.

if(flag3 == true) ...
if(flag3) ...
if(!flag3) ...
if(flag3 && flag9) ...
if(flag3 || flag9) ...

etc.

or

bool temp = flag9;
flag6 = flag3;

Notice I do not have to define any masks or use any &(ands) or |(ors) to get to my flags the compiler takes care of everything.


This method allows for rapid changes in class definition. If you don't need a flag delete it from the .h and recompile. No #defines to change

One down side is that each flag should now be dealt with explicitly. Operation like
flag1 = 0xff;
will not make the first 8 flags true.

When using Microsoft’s debugger, you may notice the value of the flag is always 0X00000000, 0X00000001, 0 or 1 depending on how you have your settings. This does not represent the flags position within the int. Use the memory viewer to see how the value of the underlying byte changes when you change a flag if you still need convincing that the flags are in fact occupying the same byte.

Questionmemory leak? Pin
derfel27-Mar-02 4:47
derfel27-Mar-02 4:47 
AnswerRe: memory leak? Pin
Patrick Hoffmann27-Mar-02 8:30
Patrick Hoffmann27-Mar-02 8:30 
AnswerRe: memory leak? Pin
6-Jun-02 15:03
suss6-Jun-02 15:03 
QuestionWhy not use...? Pin
Sven Axelsson14-Jul-01 23:38
Sven Axelsson14-Jul-01 23:38 
AnswerRe: Why not use...? Pin
Patrick Hoffmann15-Jul-01 0:31
Patrick Hoffmann15-Jul-01 0:31 
GeneralRe: Why not use...? Pin
Igor Sukhov15-Jul-01 1:03
Igor Sukhov15-Jul-01 1:03 
GeneralRe: Why not use...? Pin
Sven Axelsson15-Jul-01 1:27
Sven Axelsson15-Jul-01 1:27 
GeneralRe: Why not use...? Pin
Patrick Hoffmann15-Jul-01 1:35
Patrick Hoffmann15-Jul-01 1:35 
GeneralRe: Why not use...? Pin
15-Jul-01 9:32
suss15-Jul-01 9:32 
GeneralRe: Why not use...? Pin
Colin Leitner23-Jul-02 2:31
Colin Leitner23-Jul-02 2:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.