Bitwise comparison and enumerations / switch statements

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.
 
M

Mattias Sjögren

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
 
A

Anders Borum

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.
 
M

Mark

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top