Bitwise operators and logic...
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
But if its a boolean, I can''t track multiple states! I want to be able to track the combinations of those states.
--HB
--HB
November 09, 2000 05:20 PM
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.
// 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.
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
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
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
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.
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
Popular Topics
Advertisement
Recommended Tutorials
Advertisement