reference:
http://www.geeksforgeeks.org/position-of-rightmost-set-bit/
Problem Definition:
Write a one line C function to return position of first 1 from right to left, in binary representation of an Integer.
Solution:
Example 18(010010)
1. Take two's complement(i.e. -n) of the given no as all bits are reverted
except the first '1' from right to left (10111)
2 Do an bit-wise & with original no, this will return no with the
required one only (00010)
3 Take the log2 of the no, you will get position -1 (1)
4 Add 1 (2)
Code:
unsigned int getFirstSetBitPos(int n)
{
return log2(n&-n)+1;//or log2(n & (n^(n-1)))+1
}