How to Find first set bit



Find first set bit
Given an integer an N, write a program to print the position of first set bit found from right side in the binary representation of the number.

I/P 18, Binary Representation 010010

O/P 2

I/P 19, Binary Representation 010011

O/P 1

Can anyone explain with the help of an example


Refer this :


log2(number&(-number))+1 will give you the desired result.
Hope this help.