Bitwise comparison and enumerations / switch statements

  • Thread starter Thread starter Anders Borum
  • Start date Start date
A

Anders Borum

Hello!

Consider the following enumeration used to combine several of the values. I
am trying to figure out what the best solution is to check for the presence
of a value using bitwise comparisons. I'm able to get if statements to work,
but am unsure if switch statemens allow this.

Maybe it's just me on a friday, but if you've got a better solution than if
statements, please let me know.

[Flags()]
enum position
{
first = 1,
second = 2,
third = 4,
last = 8
}

...

// Create variable with two values from the enumeration
position p = position.first | position.second;

switch (p)
{
case position.first: { .. }
case position.second: { .. }
}

// The following if statement works
if ((p & position.first) > 0) { .. }

// The following if statement also works
if ((p & position.first) == position.first) { .. }

Thanks in advance.
 
I'm able to get if statements to work,
but am unsure if switch statemens allow this.

switch only checks if the values are equal, so you'd need one case for
every combination of the flags.



Mattias
 
Hello Mattias

That's what I expected, but I wanted to ask anyway, just in case I had
missed something. I'll pass on the info to the team. If you have suggestions
for setting up the switch case expresion, please give it a go :-)

Thanks again.
 
Back
Top