Possible Duplicate:
Efficient bitwise operations for counting bits or find the right|left most ones
Is there a fast way to find the first 1 in a (32 bit) binary number?
e.g. if I have the number 00000000 00000000 00000000 10000000
I want to calculate the value "7" (or "24", if read from the other side) since the first zero in the number is stored in the 7th bit from the right.
Is there a faster way to do this than
int pos=0;
int number = 127; //binary from above
while ((number>>pos) > 0) { pos++; }
?
Perhaps a specific x86 assembler instruction?