Is a bitmask-based check more efficient than comparing numbers?
Compare the following two methods of performing a check:
Method | Code |
---|---|
Bitmask-based | if (val & IMPORTANT_BIT) ... |
Number Comparison | if (val == IMPORTANT_VAL) ... |
In both cases, the value of val
must be fetched from memory. Does the compiler do something special with bitmask-based checks, making them more efficient than number comparisons?