Passing bitwise OR enum values

Posted Monday, June 28, 2004 11:22 PM by CumpsD
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.
Filed under:

Comments

# re: Passing bitwise OR enum values

Monday, June 28, 2004 6:01 PM by Kiliman

If you add the attribute FlagsAttribute:

[Flags] enum Pars { ... }

.NET will automatically give you the combination as string.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemflagsattributeclasstopic.asp

# re: Passing bitwise OR enum values

Monday, June 28, 2004 6:21 PM by Dhoore

nice, this is usefull :)

thx!

# re: Passing bitwise OR enum values

Tuesday, June 29, 2004 5:12 AM by David Cumps

Tnx for the attribute :) didn't knew that one

# re: Passing bitwise OR enum values

Thursday, July 01, 2004 8:42 PM by BertG

this is good info :)
maby we could teach it at school :)

# re: Setting NTFS Permissions with C#

Friday, July 23, 2004 2:21 AM by TrackBack