Search code examples
c++bit-manipulationflags

Checking for bit flags


I'm trying to check for a bit in a flags value of which flags can be |'d together. So far i'm using this

 if ((someclass.flags | CONST_SOMEFLAG) == someclass.flags)

to check if its true or false but is there a more "elegant" way of doing this?


Solution

  • That will work fine, but it's more conventional to use &:

    if (flags & MASK) . . .
    

    This is likely because on some processors testing a register for != 0 is faster than testing for equality with a stored value.