Bit Twiddling Hacks have a lot of amazing (and obscure, performance oriented) bit hacks. The best one for your use seems using multiply and lookup.
unsigned int v; // find the number of trailing zeros in 32-bit v
int r; // result goes here
static const int MultiplyDeBruijnBitPosition[32] =
{
0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
};
r = MultiplyDeBruijnBitPosition[((uint32_t)((v & -v) * 0x077CB531U)) >> 27];
This page provides a detailed analysis of the problem with a focus on chess programming.
Answer copied from here.
PS: Didn't know how to give credit to the author on that question, and so wrote it like this.