Advertisement

Bitwise operators and logic...

Started by November 09, 2000 04:58 PM
16 comments, last by HBringer 24 years, 1 month ago
Okay, here''s another simple question for you all... I''m writing some simple sprite classes (just learning DirectX); and in one I want to track the state of an object with a bitfield. Given the constants: STATE_A 1 STATE_B 2 STATE_C 4 STATE_D 8 How would I do complex logic tests, say in a switch statement? Say, for example, I want to run a switch case if both bits 1 AND 2 are set. The syntax would be: //////////////////////////////// case (STATE_A | STATE_B): //////////////////////////////// right? How about if I want to know if the variable "state" has bit 1 AND NOT bit 2 set... Wouldn''t the syntax be something like /////////////////////////// case (STATE_A & ~STATE_B): /////////////////////////// ?? Lastly, what if I want to know if bit 1 is set, bit 2 is NOT set, and bit 3 is also NOT set? This is where the syntax gets away from me... Any help would be appreciated, thanks! --HB
State should be a bool. Then you can use !STATE_B.
Advertisement
But if its a boolean, I can''t track multiple states! I want to be able to track the combinations of those states.

--HB
if( (state & (STATE_A | STATE_B) )
// STATE_A and STATE_B are set.

if( (state & STATE_A) && !(state & STATE_B) )
// STATE_A is set and STATE_B is not.

if( (state & STATE_A) && !(state & (STATE_B | STATE_C)) )
// STATE_A is set. STATE_B and STATE_C is not.

That''s it. You cannot do it in a switch statement, you''ll have to stay with if. If you use switch and lets say STATE_A, STATE_B and STATE_C is set "case (STATE_A | STATE_B:" is not true. Only "case (STATE_A | STATE_B | STATE_C ):" will be true.
Sorry, ignore my post. I misunderstood the question.
correct me if I'm wrong but I think you can't use switch statement for anything else than pure integers so you would have to use if's

The code would then be:

// set states to STATE_A and STATE_B
// to unset them use- game_state &= !STATE_A; etc.
game_state |= STATE_A;
game_state |= STATE_B;

// and to check if both A and B are set
if ((game_state & STATE_A) && (game_state & STATE_B))
// do something

// A and not B is then
if ((game_state & STATE_A) && !(game_state & STATE_B))
// do something

// the last one A and not B and not C
if ((game_state & STATE_A) && !(game_state & STATE_B) && !(game_state & STATE_C))
// do it


I think that's right

Gunnar Steinn

Edited by - GunnarSteinn on November 9, 2000 6:25:08 PM

Edited by - GunnarSteinn on November 9, 2000 6:26:51 PM
Advertisement
Hummm.... I was late
And I replied as Anonymous, sorry
-Benny-
Thanks to both of you though!

Gunnar - and some of your "&" signs need to be single; not doubled up, since its bitwise comparisons :-)

Thanks again!

--HB
Gunner is right, there needs to be logical and operators between the boolean tests.

Break it down into english:
"if (the state_a bit is set) and (the state_b bit is not set)"
=
"if (the state_a bit is set) && (the state_b bit is not set)"
= if (val & STATE_A) && !(val & STATE_B)

It uses both logical and boolean operators.

This topic is closed to new replies.

Advertisement