1304. Find Position of Only Set Bit

EasyBit ManipulationBit ManipulationPower of TwoPosition

Given an integer n, if it has exactly one set bit (that is, it is a power of two) return the 1-indexed position of that bit. Otherwise return -1.

Input: A JSON object {"n": <integer>}.

Output: Return the 1-indexed position of the single set bit, or -1.

Examples

Example 1
Input: {"n":16}
Output: 5
Explanation: 16 has one set bit at position 5.
Example 2
Input: {"n":6}
Output: -1
Explanation: 6 is 110 with two set bits, so return -1.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →