Most processors have special instructions for quickly performing many bit operations on integers such as bitwise AND, bitwise OR, bitwise XOR, bitwise NOT, left-shift and right-shift. As a result, common operations on powers of 2 can be translated to them to gain speed instead of the more costly operations.

This class of optimizations often tends to be micro-optimizations and something that the compiler often does on its own, but often a program can be restructured to take advantage on it. An example for that is having arrays whose length is a power of 2, so such bitwise operations can be performed on them.

Examples Edit

In order to multiply by 4 one can write, in C:

  int result = (x << 2);

To find the modulo by 8 one can write:

  int reminder_by_8 = (number & (8-1));