bitwise | function parameters...

Started by
10 comments, last by elis-cool 21 years, 11 months ago
Don''t use an & (AND) or an ^ (XOR) with a switch case though... stick with the | (OR) if you go with that approach.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
Advertisement
"'This will work if each of your options are independant, ie any combination of options are valid, and you having one option set won't effect any other options. Doing it this way prevents you from having to figure out the hundreds of combinations associated with with 8 flags. If that's not the case, then things get a little more complicated depending on what you need.'

I dont really get what you mean here..."

If you've got eight options that don't effect each other at all, then if statements are probably easier to work with than switch statements because you don't have to worry about combinations of options. So lets say optionA makes the sprite transparent, optionB mirrors the sprite, and optionC stretches the sprite to twice its actual size. You could do this:

      switch(selection) {case optionA:     //Make the sprite transparent     break;case optionB:     //Mirror the sprite     break;case optionC:     //Stretch the sprite     break;case optionA | optionB:     //Make the sprite transparent     //Mirror the sprite     break;case optionA | optionC:     //Make the sprite transperent     //Mirror the sprite     break;case optionB | optionC:     //Mirror the sprite     //Stretch the sprite     break;case optionA | optionB | optionC:     //Make the sprite transparent     //Mirror the sprite     //Stretch the sprite     break;}  

That's a lot of repeated code, and its really easy to forget a particual combination if you have five or six options you're trying to consider. This would be much easier:

  if(selection & optionA)    //Make the sprite transparentif(selection & optionB)    //Mirror the spriteif(selection & optionC)    //Stretch the sprite      

Because those three functions can be set in any combination, the if code is considerably easier than the switch code and provides the exact same functionality. If, though, optionA were to make the sprite red and optionB were to make the sprite green, it wouldn't work right because you can't have both optionA and optionB set at the same time. So any combination of options aren't valid. If optionA were to double the size of the sprite, and optionB were to change the stretch amount to triple the original size, then the if code wouldn't work either because you have to consider both optionA and optionB at the same time, they aren't independant of one another.

"Switches allow you to utilize fall-through and default behavior. It can end up being less code."

Agreed, but I haven't ever come across a situation where it was neccessary. So I didn't mention it. My main point, though, was that you shouldn't write one big switch statement with every logical combination of flags, because in almost all circumstances that'll be more code with additional room for errors. That seemed to be the direction elis-cool was going in, so I pointed out a more general method.

CM

[edited by - Conner McCloud on May 7, 2002 12:33:57 PM]

[edited by - Conner McCloud on May 7, 2002 12:34:30 PM]

This topic is closed to new replies.

Advertisement