If you want to check one bit mask, then
if ((value & mask) == mask)
will give you an exact match ("all bits in the mask") and
if ((value & mask) != 0)
will provide a free match ("any bit in the mask"). The compiler also optimizes checking against zero.
If you have several bitmasks, you want to extract the maximum information from each check in the time domain (extreme case: if all the values that you get are definitely odd, you do not need to check the 0 bit at all. This will always be 1). Ideally, you need to identify the first round bit, which has a 50% chance of being 1.
In both groups, you then identify a subgroup (possibly not the same in two cases) with the same probability.
if ((value & SPECIAL_MASK_1) == SPECIAL_MASK_1) { if ((value & SPECIAL_MASK_2) == SPECIAL_MASK_2) { ... } else { ... } } else { if ((value & SPECIAL_MASK_3) == SPECIAL_MASK_3) { ... } else { ... } }
If you had, say, 32 states, each of which is mapped to one bit, and only one bit can be set for each call - the simplest case - a sequential sequence would be one of 32 checks one by one
if ((mask & 0x00000001) == 0x00000001) { } else if ((mask & 0x00000002) == 0x00000002) { } ...
and the first simple optimization was to first establish checks for the most common cases. Let's say that one case out of three seventh bit is set; you first check on the seventh bit.
Thus, you will receive only one check in 33% of cases; then maybe two more checks 20% of the time ... and at the end on average you can run, say, seven checks.
Another possibility is
if (mask & 0x0000FFFF) { // The bit is in the LSW if (mask & 0x0000FF00) { // MSB of LSW if (mask & 0x0000F000) { ... } else { } } } else { }
This will be performed exactly five checks each time. However, at this point, considerations about CPU architecture, branch prediction, etc. will probably surpass any optimization you can try to do.
If you have a very complicated setup or some other restriction (for example, an embedded device), I am afraid that the cost of analyzing, building, debugging and maintaining an “optimized” or “brute force” test will most likely be more balanced any advantage that you could squeeze out of the first.