Click here to Skip to main content
Licence 
First Posted 14 Jul 2001
Views 77,476
Downloads 413
Bookmarked 21 times

Store more than 32 1-bit-flags in a single 'scalar'

By | 14 Jul 2001 | Article
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 anymore than that!", I told him. He didn't believe me and discards 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 manage your flags and rely 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

About the Author

Patrick Hoffmann

Web Developer

Germany Germany

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralBigFloat... PinmemberTHANH VAN6:58 5 Jun '02  
GeneralAnother approach PinmemberMaroy7:16 15 May '02  
Questionmemory leak? Pinmemberderfel4:47 27 Mar '02  
AnswerRe: memory leak? PinmemberPatrick Hoffmann8:30 27 Mar '02  
AnswerRe: memory leak? PinmemberAnonymous15:03 6 Jun '02  
QuestionWhy not use...? PinmemberSven Axelsson23:38 14 Jul '01  
AnswerRe: Why not use...? PinmemberPatrick Hoffmann0:31 15 Jul '01  
GeneralRe: Why not use...? PinmemberIgor Soukhov1:03 15 Jul '01  
GeneralRe: Why not use...? PinmemberSven Axelsson1:27 15 Jul '01  
GeneralRe: Why not use...? PinmemberPatrick Hoffmann1:35 15 Jul '01  
GeneralRe: Why not use...? PinmemberPaul A. Howes9:32 15 Jul '01  
GeneralRe: Why not use...? PinsussColin Leitner2:31 23 Jul '02  

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 15 Jul 2001
Article Copyright 2001 by Patrick Hoffmann
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid