Kevin Trethewey

Software Developer, Technologist, Connoisseur of things that go 'bing'.

  • Home
  • Contact
  • Links
  • About

The implementation of Flags using bitwise operators

Published Monday, April 19, 2004 3:20 PM

The implementation of Flags using bitwise operators



Introduction:
Bitwise operators give you the ability to store multiple settings in a single primitive data type (e.g. an integer). This is useful when a single item has potentially more than one setting of the same type. For example, a message box could have an Ok button, a Cancel button, a Retry button or any combination of the above. By assigning a flag to each button, the complete combination of which buttons to show can be represented in a single primitive data type. Unlike all other articles i have seen in on this topic, i am not going to delve into exactly how this works (bit manipulation), but instead concentrate on how to implement the functionality in C#, VB.NET and then SQL. If you would like to understand how it all works, start here.



Sections:

C# Implementation
VB.NET Implementation
TSQL Implementation



C# Implementation



Defining the Flags:
To start of with, declare an enum to list all the possible flags. Two things are important when declaring the enum. The first thing you will probably notice is the [Flags] attribute. This is necessary in order to indicate that the enumeration should be treated as a set of flags. The second important thing is assigning a value to each of the items in the enum. The first value should be 1, then just double the value for each consecutive item. The integer type in .NET can store up to 32 flags.

[Flags] private enum Buttons : int
{
···Ok = 1, Cancel = 2, Retry = 4, Help = 8
}


Tip: An "All" item could be added to the list of items in the enumeration as follows: All = Ok | Cancel | Retry | Help


Setting flags ON:
To set multiple flags, concatenate the desired flags using the bitwise OR symbol "|":

Buttons buttons;
buttons = Buttons.Ok | Buttons.Cancel;


Setting flags OFF:

buttons &= ~Buttons.Cancel;


Testing to see if a certain flag is set:

if ((buttons&Buttons.Ok)==Buttons.Ok)
···Console.WriteLine("Ok");



VB.NET Implementation


(This section in the same as the C# section, the examples are just in VB.)

Defining the Flags:
To start of with, declare an enum to list all the possible flags. Two things are important when declaring the enum. The first thing you will probably notice is the [Flags] attribute. This is necessary in order to indicate that the enumeration should be treated as a set of flags. The second important thing is assigning a value to each of the items in the enum. The first value should be 1, then just double the value for each consecutive item. The integer type in .NET can store up to 32 flags.

<Flags()> _
Private Enum Buttons As Integer
···Ok = 1
···Cancel = 2
···Retry = 4
···Help = 8
End Enum


Tip: An "All" item could be added to the list of items in the enumeration as follows: All = Ok And Cancel And Retry And Help


Setting flags ON:
To set multiple flags, concatenate the desired flags using the Or keyword:

Dim btns As Buttons
btns = Buttons.Ok Or Buttons.Cancel


Setting flags OFF:

btns = btns And Not Buttons.Cancel


Testing to see if a certain flag is set:

If (btns And Buttons.Ok) = Buttons.Ok Then
···Console.WriteLine("Ok")
End If



TSQL Implementation



Of course there are no enumeration constructs in TSQL, so the script required to implement flags is a bit rougher than in .NET, but it works just as well. This section anticipates that you would have already looked at one of the two sections above before you work through this one...

Setting flag 1 ON:

declare @MyFlags int
select @MyFlags = IsNull(@MyFlags, 0) | 1



Setting flag 1 OFF:

select @MyFlags = IsNull(@MyFlags, 0) ^ 1



Testing to see if flag 1 is set:

if (IsNull(@MyFlags, 0) & 1) <> 0
···select 'Yes'
else
···select 'No'




Finally...


That's all there is to it! I have attached source code / script samples, if you are unclear on anything, work through these samples and it should all make sense.

[01 May 08 : Fixed error in TSQL implementation, thank Pepijn van de Kamp]
Share this post: email it! | bookmark it! | digg it! | reddit! | kick it! | live it!
by Kevin Trethewey
Filed under: Software Development

Comments

# TrackBack said on Monday, April 19, 2004 3:32 PM
# Chris said on Wednesday, May 11, 2005 8:12 PM

The article was great! thanks but comments about useless s hit?

Possibly time to change your verification picture generating method, to stop automated posts?!

# KevinT said on Wednesday, May 11, 2005 8:22 PM

Thanks Chris - sorry, these came up before we implemented the verification (they were the reason behind it in fact). I must have missed them when i did a clean-up. I will delete them now...

# TrackBack said on Monday, June 27, 2005 11:44 AM

Sollte man ansich wissen, mangels regelmäßiger Benutzung aber irgendwie schwer zu merken Hier zwei seiten die das Thema sehr gut behandeln, letzteres dürfte wohl häufiger nötig sein.

- Masks and flags using bit fields in .NET
- csharpThe implementati

# Andrew said on Tuesday, February 28, 2006 9:30 PM

Thanks, it was very informational!

# Edison Capiz said on Monday, March 27, 2006 4:06 AM

Wow! just when I really needed such an article..
Thanks a lot.

# edotcom said on Friday, April 14, 2006 1:24 PM

cheers dude, you saved me some time there! :P

# Stephen said on Friday, April 14, 2006 6:20 PM

Thank you! I'm new to bitwise flags. I spent over 2 hours in the .net help system plus failed attempts before finding your site.

MS does not illustrate a simple method for testing to see if a certain flag is set. For the flag enum, MS gives you everything you really don't need such as playing with casting, base object, hash tables, dictionaries, etc... but doesn't show the vital basic flag test...


Thanks again

# DotNetKicks.com said on Monday, August 11, 2008 8:12 AM

You've been kicked (a good thing) - Trackback from DotNetKicks.com

Go

Tags

  • .NET
  • Admin
  • ALT.NET
  • Community
  • Dotnet.org.za
  • eBooks
  • Fonts
  • Free Stuff
  • Fun Stuff
  • Google
  • Links
  • Microsoft
  • Personal OffTopic
  • Quotes
  • Red Five Labs
  • Robotics
  • Software Development
  • South Africa
  • SQL
  • Useful Advice
  • Utilities
  • Video
  • Virtual Post-It
  • Vista
  • VSTS Tips
  • WebDev
  • Wiki

Navigation

  • Home
  • Careers
  • Downloads
  • Support

Archives

  • May 2008 (1)
  • October 2007 (1)
  • July 2007 (1)
  • June 2007 (2)
  • May 2007 (1)
  • February 2007 (2)
  • January 2007 (2)
  • December 2006 (3)
  • November 2006 (9)
  • October 2006 (10)
  • September 2006 (3)
  • August 2006 (2)
  • July 2006 (6)
  • June 2006 (7)
  • May 2006 (9)
  • April 2006 (4)
  • March 2006 (14)
  • February 2006 (2)
  • January 2006 (5)
  • December 2005 (5)
  • November 2005 (7)
  • October 2005 (4)
  • September 2005 (10)
  • August 2005 (3)
  • July 2005 (5)
  • June 2005 (8)
  • May 2005 (11)
  • April 2005 (10)
  • March 2005 (7)
  • February 2005 (11)
  • January 2005 (12)
  • December 2004 (5)
  • November 2004 (2)
  • October 2004 (3)
  • September 2004 (10)
  • August 2004 (13)
  • July 2004 (6)
  • June 2004 (7)
  • May 2004 (24)
  • April 2004 (20)
  • March 2004 (21)
  • February 2004 (19)

Syndication

  • RSS
  • Atom
  • Comments RSS
Powered by Community Server (Commercial Edition), by Telligent Systems