Testing multiple bit flags at once?

Just starting out? Need help? Post your questions and find answers here.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Testing multiple bit flags at once?

Post by Mistrel »

I have a set of flags that I want to test. I can't do the following because it will debug "1" for either TEST_A or TEST_B and not both exclusively:

Code: Select all

#TEST_A=1<<0
#TEST_B=1<<1

a|#TEST_A

If a&(#TEST_A|#TEST_B)
  Debug 1
EndIf
I have to do this instead:

Code: Select all

If a&#TEST_A And a&#TEST_B
  Debug 2
EndIf
Is there a way to test whether all flags exist in a single operation without having to use multiple checks in this way?
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Testing multiple bit flags at once?

Post by wilbert »

Mistrel wrote:Is there a way to test whether all flags exist in a single operation without having to use multiple checks in this way?
You can compare against the mask

Code: Select all

#TEST_A=1<<0
#TEST_B=1<<1

a = #TEST_A|#TEST_B

If a & (#TEST_A|#TEST_B) = #TEST_A|#TEST_B
  Debug "Both flags are set"
EndIf
Windows (x64)
Raspberry Pi OS (Arm64)
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: Testing multiple bit flags at once?

Post by BarryG »

Building on Wilbert's code, you can use a macro for less typing and better readability:

Code: Select all

#TEST_A=1<<0
#TEST_B=1<<1

Macro HasTheseFlags(f)
  & (f) = (f)
EndMacro

If a HasTheseFlags(#TEST_A|#TEST_B)
  Debug "Both flags are set"
EndIf
Last edited by BarryG on Sun Oct 13, 2019 8:57 am, edited 1 time in total.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Re: Testing multiple bit flags at once?

Post by Mistrel »

Maybe I'm just getting old to be asking questions like this. Thank you all for putting up with my rants where I talk big and then come and ask silly questions like this. :oops:

It's been a while since I've worked with bit flags. I feel very spoiled by object-oriented enum classes and enum sets.
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: Testing multiple bit flags at once?

Post by BarryG »

I'm just glad I could help! I don't use bits very often, so I'm shocked that my macro actually works.
User avatar
STARGÅTE
Addict
Addict
Posts: 2067
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Testing multiple bit flags at once?

Post by STARGÅTE »

BarryG wrote: I'm just glad I could help! I don't use bits very often, so I'm shocked that my macro actually works.
It doesn't.

Code: Select all

#TEST_A=1<<0
#TEST_B=1<<1

Macro HasTheseFlags(f)
  & f = f
EndMacro

a = #TEST_A

If a HasTheseFlags(#TEST_A|#TEST_B)
  Debug "Both flags are set"
EndIf
You have to use brackets:

Code: Select all

Macro HasTheseFlags(f)
  & (f) = (f)
EndMacro
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: Testing multiple bit flags at once?

Post by BarryG »

Thank you, STARGÅTE. Post corrected.
Post Reply