Given a number having only one ‘1’ and all other ’0’s in its binary representation, find position of the only set bit. Source: Microsoft Interview | 18
We strongly recommend that you click here and practice it, before moving on to the solution.
The idea is to start from rightmost bit and one by one check value of every bit. Following is detailed algorithm.

1) If number is power of two then and then only its binary representation contains only one ‘1’. That’s why check whether given number is power of 2 or not. If given number is not power of 2, then print error message and exit.

2) Initialize two variables; i = 1 (for looping) and pos = 1 (to find position of set bit)

3) Inside loop, do bitwise AND of i and number ‘N’. If value of this operation is true, then “pos” bit is set, so break the loop and return position. Otherwise, increment “pos” by 1 and left shift i by 1 and repeat the procedure.

[pastacode lang=”c” manual=”%2F%2F%20C%20program%20to%20find%20position%20of%20only%20set%20bit%20in%20a%20given%20number%0A%23include%20%3Cstdio.h%3E%0A%20%0A%2F%2F%20A%20utility%20function%20to%20check%20whether%20n%20is%20power%20of%202%20or%20not.%20See%20http%3A%2F%2Fgoo.gl%2F17Arj%0Aint%20isPowerOfTwo(unsigned%20n)%0A%7B%20%20return%20n%20%26%26%20(!%20(n%20%26%20(n-1))%20)%3B%20%7D%0A%20%0A%2F%2F%20Returns%20position%20of%20the%20only%20set%20bit%20in%20’n’%0Aint%20findPosition(unsigned%20n)%0A%7B%0A%20%20%20%20if%20(!isPowerOfTwo(n))%0A%20%20%20%20%20%20%20%20return%20-1%3B%0A%20%0A%20%20%20%20unsigned%20i%20%3D%201%2C%20pos%20%3D%201%3B%0A%20%0A%20%20%20%20%2F%2F%20Iterate%20through%20bits%20of%20n%20till%20we%20find%20a%20set%20bit%0A%20%20%20%20%2F%2F%20i%26n%20will%20be%20non-zero%20only%20when%20’i’%20and%20’n’%20have%20a%20set%20bit%0A%20%20%20%20%2F%2F%20at%20same%20position%0A%20%20%20%20while%20(!(i%20%26%20n))%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20Unset%20current%20bit%20and%20set%20the%20next%20bit%20in%20’i’%0A%20%20%20%20%20%20%20%20i%20%3D%20i%20%3C%3C%201%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20increment%20position%0A%20%20%20%20%20%20%20%20%2B%2Bpos%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20return%20pos%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%20function%0Aint%20main(void)%0A%7B%0A%20%20%20%20int%20n%20%3D%2016%3B%0A%20%20%20%20int%20pos%20%3D%20findPosition(n)%3B%0A%20%20%20%20(pos%20%3D%3D%20-1)%3F%20printf(%22n%20%3D%20%25d%2C%20Invalid%20number%5Cn%22%2C%20n)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20printf(%22n%20%3D%20%25d%2C%20Position%20%25d%20%5Cn%22%2C%20n%2C%20pos)%3B%0A%20%0A%20%20%20%20n%20%3D%2012%3B%0A%20%20%20%20pos%20%3D%20findPosition(n)%3B%0A%20%20%20%20(pos%20%3D%3D%20-1)%3F%20printf(%22n%20%3D%20%25d%2C%20Invalid%20number%5Cn%22%2C%20n)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20printf(%22n%20%3D%20%25d%2C%20Position%20%25d%20%5Cn%22%2C%20n%2C%20pos)%3B%0A%20%0A%20%20%20%20n%20%3D%20128%3B%0A%20%20%20%20pos%20%3D%20findPosition(n)%3B%0A%20%20%20%20(pos%20%3D%3D%20-1)%3F%20printf(%22n%20%3D%20%25d%2C%20Invalid%20number%5Cn%22%2C%20n)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20printf(%22n%20%3D%20%25d%2C%20Position%20%25d%20%5Cn%22%2C%20n%2C%20pos)%3B%0A%20%0A%20%20%20%20return%200%3B%0A%7D” message=”C Programming” highlight=”” provider=”manual”/]

Output:

n = 16, Position 5
n = 12, Invalid number
n = 128, Position 8
[ad type=”banner”]

Following is another method for this problem. The idea is to one by one right shift the set bit of given number ‘n’ until ‘n’ becomes 0. Count how many times we shifted to make ‘n’ zero. The final count is position of the set bit.

[pastacode lang=”c” manual=”%2F%2F%20C%20program%20to%20find%20position%20of%20only%20set%20bit%20in%20a%20given%20number%0A%23include%20%3Cstdio.h%3E%0A%20%0A%2F%2F%20A%20utility%20function%20to%20check%20whether%20n%20is%20power%20of%202%20or%20not%0Aint%20isPowerOfTwo(unsigned%20n)%0A%7B%20%20return%20n%20%26%26%20(!%20(n%20%26%20(n-1))%20)%3B%20%7D%0A%20%0A%2F%2F%20Returns%20position%20of%20the%20only%20set%20bit%20in%20’n’%0Aint%20findPosition(unsigned%20n)%0A%7B%0A%20%20%20%20if%20(!isPowerOfTwo(n))%0A%20%20%20%20%20%20%20%20return%20-1%3B%0A%20%0A%20%20%20%20unsigned%20count%20%3D%200%3B%0A%20%0A%20%20%20%20%2F%2F%20One%20by%20one%20move%20the%20only%20set%20bit%20to%20right%20till%20it%20reaches%20end%0A%20%20%20%20while%20(n)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20n%20%3D%20n%20%3E%3E%201%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20increment%20count%20of%20shifts%0A%20%20%20%20%20%20%20%20%2B%2Bcount%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20return%20count%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%20function%0Aint%20main(void)%0A%7B%0A%20%20%20%20int%20n%20%3D%200%3B%0A%20%20%20%20int%20pos%20%3D%20findPosition(n)%3B%0A%20%20%20%20(pos%20%3D%3D%20-1)%3F%20printf(%22n%20%3D%20%25d%2C%20Invalid%20number%5Cn%22%2C%20n)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20printf(%22n%20%3D%20%25d%2C%20Position%20%25d%20%5Cn%22%2C%20n%2C%20pos)%3B%0A%20%0A%20%20%20%20n%20%3D%2012%3B%0A%20%20%20%20pos%20%3D%20findPosition(n)%3B%0A%20%20%20%20(pos%20%3D%3D%20-1)%3F%20printf(%22n%20%3D%20%25d%2C%20Invalid%20number%5Cn%22%2C%20n)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20printf(%22n%20%3D%20%25d%2C%20Position%20%25d%20%5Cn%22%2C%20n%2C%20pos)%3B%0A%20%0A%20%20%20%20n%20%3D%20128%3B%0A%20%20%20%20pos%20%3D%20findPosition(n)%3B%0A%20%20%20%20(pos%20%3D%3D%20-1)%3F%20printf(%22n%20%3D%20%25d%2C%20Invalid%20number%5Cn%22%2C%20n)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20printf(%22n%20%3D%20%25d%2C%20Position%20%25d%20%5Cn%22%2C%20n%2C%20pos)%3B%0A%20%0A%20%20%20%20return%200%3B%0A%7D” message=”C Programming” highlight=”” provider=”manual”/]

Output:

n = 0, Invalid number
n = 12, Invalid number
n = 128, Position 8

We can also use log base 2 to find the position.

[pastacode lang=”c” manual=”%23include%20%3Cstdio.h%3E%0A%20%0Aunsigned%20int%20Log2n(unsigned%20int%20n)%0A%7B%0A%20%20%20return%20(n%20%3E%201)%3F%201%20%2B%20Log2n(n%2F2)%3A%200%3B%0A%7D%0A%20%0Aint%20isPowerOfTwo(unsigned%20n)%0A%7B%0A%20%20%20%20return%20n%20%26%26%20(!%20(n%20%26%20(n-1))%20)%3B%0A%7D%0A%20%0Aint%20findPosition(unsigned%20n)%0A%7B%0A%20%20%20%20if%20(!isPowerOfTwo(n))%0A%20%20%20%20%20%20%20%20return%20-1%3B%0A%20%20%20%20return%20Log2n(n)%20%2B%201%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%20function%0Aint%20main(void)%0A%7B%0A%20%20%20%20int%20n%20%3D%200%3B%0A%20%20%20%20int%20pos%20%3D%20findPosition(n)%3B%0A%20%20%20%20(pos%20%3D%3D%20-1)%3F%20printf(%22n%20%3D%20%25d%2C%20Invalid%20number%5Cn%22%2C%20n)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20printf(%22n%20%3D%20%25d%2C%20Position%20%25d%20%5Cn%22%2C%20n%2C%20pos)%3B%0A%20%0A%20%20%20%20n%20%3D%2012%3B%0A%20%20%20%20pos%20%3D%20findPosition(n)%3B%0A%20%20%20%20(pos%20%3D%3D%20-1)%3F%20printf(%22n%20%3D%20%25d%2C%20Invalid%20number%5Cn%22%2C%20n)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20printf(%22n%20%3D%20%25d%2C%20Position%20%25d%20%5Cn%22%2C%20n%2C%20pos)%3B%0A%20%0A%20%20%20%20n%20%3D%20128%3B%0A%20%20%20%20pos%20%3D%20findPosition(n)%3B%0A%20%20%20%20(pos%20%3D%3D%20-1)%3F%20printf(%22n%20%3D%20%25d%2C%20Invalid%20number%5Cn%22%2C%20n)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20printf(%22n%20%3D%20%25d%2C%20Position%20%25d%20%5Cn%22%2C%20n%2C%20pos)%3B%0A%20%0A%20%20%20%20return%200%3B%0A%7D” message=”C Programming” highlight=”” provider=”manual”/]

Output:

n = 0, Invalid number
n = 12, Invalid number
n = 128, Position 8
[ad type=”banner”]

Categorized in: