log2 function

int log2 (int number)

Evaluate the binary logarithm of an integer number.

Two-step method using a De Bruijn-like sequence table lookup.

Implementation

int log2(int number) {
  number |= number >> 1;
  number |= number >> 2;
  number |= number >> 4;
  number |= number >> 8;
  number |= number >> 16;
  number |= number >> 32;

  return multiplyDeBruijnBitPosition[
      ((number - (number >> 1)) * 0x07EDD5E59A4E28C2) >> 58];
}