countTrailingZeroBits function

int countTrailingZeroBits(
  1. int v
)

Count the consecutive zero bits (trailing) on the right in parallel https://graphics.stanford.edu/~seander/bithacks.html#ZerosOnRightParallel

Implementation

int countTrailingZeroBits(int v) {
  var c = 32;
  v &= -v;
  if (v != 0) c--;
  if (v & 0x0000ffff != 0) c -= 16;
  if (v & 0x00ff00ff != 0) c -= 8;
  if (v & 0x0f0f0f0f != 0) c -= 4;
  if (v & 0x33333333 != 0) c -= 2;
  if (v & 0x55555555 != 0) c -= 1;
  return c;
}