Click here to Skip to main content
15,884,298 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 97.9K   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 
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.