quick question here...
What is the difference between...
if ((flags & bit1) == bit1) {
// ...
}
and...
if (flags & bit1) {
// ...
}
?
That's all. Pretty sure this has been answered before, but I haven't been able to find it.
CodePudding user response:
The first checks whether flags
has all of the bits set where bit1
is also set. The second checks whether flags
has any (i.e. at least one) of the bits set where bit1
is also set (or vice versa; in other words, whether there's any common set bits). If bit1
has a single bit set, then there is no difference between "any" and "all".
CodePudding user response:
The first checks if that particular bitmask is set.
the second checks if any bit in that bitmask is set
int main() {
int bitpattern = 0b00110110;
int mask = 0b00111111;
if(bitpattern & mask) {
// the bits can be 0b00110110
// or 0b00110111,0b00111110,0b00111111
// or 0b00000110 and so on
}
if((bitpattern & mask) == bitpattern ) {
// the bits are EXACTLY 0b00110110
}
}