The Artima Developer Community
Sponsored Link

.NET Buzz Forum
Passing bitwise OR enum values

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
David Cumps

Posts: 319
Nickname: cumpsd
Registered: Feb, 2004

David Cumps is a Belgian Student learning .NET
Passing bitwise OR enum values Posted: Jun 28, 2004 3:05 PM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by David Cumps.
Original Post: Passing bitwise OR enum values
Feed Title: David Cumps
Feed URL: http://weblogs.asp.net/cumpsd/rss?containerid=12
Feed Description: A Student .Net Blog :p
Latest .NET Buzz Posts
Latest .NET Buzz Posts by David Cumps
Latest Posts From David Cumps

Advertisement
Everyone who has ever used a Regex knows this form of parameter:
1Regex r = new Regex("", RegexOptions.Singleline | RegexOptions.IgnoreCase);
I often wondered how to do that as well and because nobody ever taught me that at school, I had to figure it out myself. And today I did, and I'm sharing :)

This is very usefull if you have a class that takes a lot of options and you don't want to add a bool for each possible option.

First you need to create an enum with the possible options, and number them binary. (eg: 1, 2, 4, 8, 16, ...)
1  public enum Pars {

2 One = 1,
3 Two = 2,
4 Three = 4,
5 Four = 8,
6 Five = 16,
7 Six = 32,
8 Seven = 64,
9 } /* Pars */
Next you need to have a method which takes this enum as a parameter, after which you can extract each of the possible options from it with a bitwise AND:
1  private static void TestPars(Pars options) {

2 if ((options & Pars.One) == Pars.One) { Console.WriteLine("One"); }
3 if ((options & Pars.Two) == Pars.Two) { Console.WriteLine("Two"); }
4 if ((options & Pars.Three) == Pars.Three) { Console.WriteLine("Three"); }
5 if ((options & Pars.Four) == Pars.Four) { Console.WriteLine("Four"); }
6 if ((options & Pars.Five) == Pars.Five) { Console.WriteLine("Five"); }
7 if ((options & Pars.Six) == Pars.Six) { Console.WriteLine("Six"); }
8 if ((options & Pars.Seven) == Pars.Seven) { Console.WriteLine("Seven"); }
9 } /* TestPars */
When all this is done, you call the method, passing the options you want with a bitwise OR between them, like this:
1  static void Main(string[] args) {

2 TestPars(Pars.Three | Pars.Five | Pars.Seven);
3 }
This example will print Three, Five and Seven.

How does it work internally?

Here are the rules for the bitwise operators:

Bitwise OR:
0101 (expression1)
1100 (expression2)
----
1101 (result)

Bitwise AND:
0101 (expression1)
1100 (expression2)
----
0100 (result)

Now, our enum has the following binary values:

1 - 0001
2 - 0010
3 - 0100
4 - 1000
...

If for example we pass the options One and Two along, we combine them with the bitwise OR, creating 0011.

And if we want to check if Four was passed along, in our if we check if the options combined bitwise with Four result in Four.

0011
0100
----
0000

Four was not passed.

If we check with Two we get the following:

0011
0010
----
0010

And 0010 = Two, our if = true, and Two was passed along.

A lot of you will say "well duh, that's basics". Could be true, but I never learnt it at school, and never ever had the need for it, but now I wanted to know, and I guess there are more people out there who haven't learnt it either.

Read: Passing bitwise OR enum values

Topic: Visual Studio 2005 Beta Ready to Roll Previous Topic   Next Topic Topic: Hot New Pocket PC (in a 1.5 ton Case...)

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use