Search code examples
language-agnosticbit-manipulationbit

What USEFUL bitwise operator code tricks should a developer know about?


I must say I have never had cause to use bitwise operators, but I am sure there are some operations that I have performed that would have been more efficiently done with them. How have "shifting" and "OR-ing" helped you solve a problem more efficiently?


Solution

  • See the famous Bit Twiddling Hacks
    Most of the multiply/divide ones are unnecessary - the compiler will do that automatically and you will just confuse people.

    But there are a bunch of, 'check/set/toggle bit N' type hacks that are very useful if you work with hardware or communications protocols.