Suppose x
is a bitmask value, and b
is one flag, e.g.
x = 0b10101101
b = 0b00000100
There seems to be two ways to check whether the bit indicated by b
is turned on in x
:
if (x & b != 0) // (1)
if (x & b == b) // (2)
In most circumstances it seems these two checks always yield the same result, given that b
is always a binary with only one bit turned on.
However I wonder is there any exception that makes one method better than another?