Question: I am using inotify and want to efficiently check against the reported bitmask event (see [inotify man page](http://linux.die.net/man/7/inotify)).

Now I could brutely check against every bit on every event, but that would be extremely crude, if not stupid, as I would have N conditionals every time. Or is calling

```
( bitmask & mask ) == mask

```

for **each** mask already super efficient?

Since the resulting bitmask is basically just a well defined number, I should be able to use basic arithmetic operations for this. But before I think up something myself, I wanted to ask if there is a well-known, efficient way to check against a given bitmask. So, is there?

Answer: 
For checking multiple bits of a mask I use a loop. If you're using a decent compiler it should optimise the code fairly decently. Unless you have significant performance problems, it's not worth hand-optimising as all the CPUs I know of implement a logical bit test or bit-wise AND operation in a single instruction. So you have two instructions: the logic instruction and the CPU branch-condition instruction for each bit. Not a massive amount of code to run and - as far as I know - impossible to beat. (Note that since the `mask` is 32 bits wide, if you're running on a 16 bit core CPU, there will be a couple more instructions to test both halves.)

```
void processEvents(uint32_t events)
{
    uint32_t bitToTest;
    // Check each bit in turn
    for(bitToTest = 1; bitToTest < events; bitToTest << 1)
    {
        // Check which bit is set.  If none then the default case is used.
        switch(bitToTest & events)
        {
            case IN_ACCESS:
                // Handle the IN_ACCESS event flag here.
                break;
            case IN_ATTRIB:
                // Handle the IN_ATTRIB event flag here.
                break;
            // Et cetera...
            default:
                // No flag was set, so do nothing.
                break;
        }
    }
}

```
If it is necessary to check against every bit mask, there is no other way than to check explicitly. However, if the specific bit masks are known, bitwise checks could be done, effectively resulting in ruling out half of the possible bit masks in every step.