finding the first set bit in a binary number

bit-manipulation, c++

Solution

If you want it to be fast, bitscan instruction (`bsf`, `bsr`) or bit-twiddling hack is the target to go.

EDIT: The idea of using switch-case table to improve performance is nothing but immature.

Problem

I need to find the first set bit in a binary number from right to left; I came up with this solution: ``` int cnt=0; while (number& 1 ==0) { cnt++; number>>=1; } ``` Is there a better way of doing it? Some clever bit manipulation technique?

Original source

Related problems