nextPowerOf2 function

int nextPowerOf2(
  1. int x
)

Returns the smallest power of two equal or greater than x.

Implementation

int nextPowerOf2(int x) {
  --x;
  x |= x >> 1;
  x |= x >> 2;
  x |= x >> 4;
  x |= x >> 8;
  x |= x >> 16;
  x |= x >> 32;
  ++x;
  return x;
}