
Passing Bitwise OR Enum Values
Originally published on my old blog at cumps.be on June 28, 2004.
Everyone who has ever used a Regex knows this form of parameter:
Regex 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 useful 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 as powers of two (e.g. 1, 2, 4, 8, 16, …).
public enum Pars {
One = 1,
Two = 2,
Three = 4,
Four = 8,
Five = 16,
Six = 32,
Seven = 64
} /* 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:
private static void TestPars(Pars options) {
if ((options & Pars.One) == Pars.One) { Console.WriteLine("One"); }
if ((options & Pars.Two) == Pars.Two) { Console.WriteLine("Two"); }
if ((options & Pars.Three) == Pars.Three) { Console.WriteLine("Three"); }
if ((options & Pars.Four) == Pars.Four) { Console.WriteLine("Four"); }
if ((options & Pars.Five) == Pars.Five) { Console.WriteLine("Five"); }
if ((options & Pars.Six) == Pars.Six) { Console.WriteLine("Six"); }
if ((options & Pars.Seven) == Pars.Seven) { Console.WriteLine("Seven"); }
} /* TestPars */
When all this is done, you call the method, passing the options you want with a bitwise OR between them, like this:
static void Main(string[] args) {
TestPars(Pars.Three | Pars.Five | Pars.Seven);
}
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:
One - 0001
Two - 0010
Three - 0100
Four - 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 Three was passed along, in our if we check whether the options ANDed with Three result in Three.
0011
0100
----
0000
Three 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 learned 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 learned it either.
You May Also Like

Error Reporting to the EventLog with NUnit
Building a C# class that reads and writes the Windows EventLog, test-driven with NUnit, from failing setup to passing tests.

FrontPage Server Extensions and the IIS Metabase
Setting FrontPageWeb in the IIS metabase from C# doesn't install FrontPage Server Extensions; running owsadm.exe from KB 300543 does.

Parameterized Queries with MySQL
Switching to parameterized queries with MySQL and MyODBC: don't put quotes around the ? placeholders, or you insert a question mark.



