Click here to Skip to main content
Licence CPOL
First Posted 16 Sep 2008
Views 9,450
Downloads 43
Bookmarked 12 times

Flag Group

By | 16 Sep 2008 | Article
A structure that stores up to 32 booleans in a 32-bit integer.

FlagGroup_src

Introduction

This is a simple structure which stores up to 32 booleans in a 32-bit integer. It is useful for combining many boolean settings into one, two, or four bytes, ready to be saved in a file. Each flag/bit can be accessed using a method or an indexer.

Background

When I create applications with configuration files, I frequently like to keep boolean settings in just one byte or more. I created this structure to allow me to set which bits were on or off, and return a byte/short/integer to save in my configuration file.

Using the code

Flags or bits can be accessed using the set or get methods, or by using the indexer property.

//Declare flag group
FlagGroup myFlags = new FlagGroup();

//Set flag
myFlags.SetFlag(12, true);
myFlags[12] = true;

//Get flag
bool f = myFlags.GetFlag(12);
f = myFlags[12];

There are three properties which allow you to get or set the first byte, or the first two bytes, or all four bytes.

//Set/get group
myFlags.Int32 = 0xFFFFFFFF;
myFlags.Int16 = 0xAA;
myFlags.Int8 = 0;

The ToString() method will return a string containing a binary representation of the flag group.

Implementing the structure

In the demo archive, you will find a DLL of the FlagGroup class in the namespace Ted. This should be added as a reference to your own projects. However, since there is very little code, you might prefer to add the code file to the project, which you will find in the source archive.

If you have any other questions or comments, then please feel free to contact me.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Ted John



United Kingdom United Kingdom

Member

Ted John started programming at an early age using Visual Basic and HTML. As he gained more experience, he starting writing utilities for his favourite video games.
 
In his spare time he currently writes program for fun in mostly VB.NET, C# and C++. He also runs a website in PHP and a wiki.
 
He also enjoys playing strategy games such as popular Tycoon games and Populous: The Beginning (his favourite game).

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
Generalwooo :D Pinmemberpredator_214:30 4 Feb '10  
GeneralBit flags PinmemberMorbidCamel21:28 16 Sep '08  
GeneralRe: Bit flags PinmemberTed John0:40 17 Sep '08  
GeneralRe: Bit flags PinmemberPIEBALDconsult3:52 17 Sep '08  
GeneralSpeed Pinmembersk8er_boy28720:05 16 Sep '08  
GeneralRe: Speed PinmemberTed John0:33 17 Sep '08  
RantRe: Speed Pinmembersk8er_boy2873:21 17 Sep '08  
GeneralAnother alternative PinmemberBen Slater17:53 16 Sep '08  

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
Web02 | 2.5.120517.1 | Last Updated 16 Sep 2008
Article Copyright 2008 by Ted John
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid