If n has exactly one bit set (i.e. it is a power of two), output the 1-indexed position of that bit counting from the least significant bit (so 1 -> position 1, 2 -> position 2, 4 -> position 3). Otherwise output -1.
One line: a positive integer n.
One line: the 1-indexed position of the single set bit, or -1.
Example 1
Input
16
Expected
5
Explanation
16 = 2^4 has one set bit at position 5 (counting from 1).
Example 2
Input
6
Expected
-1
Explanation
6 = 110 has two set bits, so the answer is -1.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →