A simple way of finding this number is as follows:
int findLowestSetBit(int n) {
for (int i = 0 ; i != 32 ; i++) {
if ((n & (1 << i)) != 0) {
return i;
}
}
return -1;
}
However, it is not the fastest one, because it searches for a set bit "linearly". You can do it in parallel with the following piece of code copied from the bit hack page:
int v; // 32-bit word input to count zero bits on right
int c = 32; // c will be the number of zero bits on the right
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;