Bit Manipulation · Easy
223. Find the Position of the Only Set Bit
Topics: Bit Manipulation
Solve it on DeVert Campus - free code runner
Problem
Given an integer n that is guaranteed to be a power of 2 (so it has exactly one set bit), find the 1-indexed position of that bit, counting from the least significant bit as position 1. If n does not have exactly one set bit, print -1.
Input and output
Input: A single line contains the integer n.
Output: Print a single integer - the 1-indexed position (from the LSB) of n's only set bit, or -1 if n doesn't have exactly one set bit.
Example
Input: 16 Output: 5
Constraints
0 <= n <= 10^9
Key observation
This problem directly reuses and builds on the exact validation check from the earlier 'Check if a Number Is a Power of 2' problem - recognizing that reuse is itself a useful interview signal that you're building a mental toolkit of bit tricks, not solving each problem from scratch.
Intuition
Given a number that's guaranteed to have exactly one '1' in its binary form, find WHICH position that 1 is sitting in, counting from the rightmost bit as position 1.
Brute force: Convert the number to a binary string and find the index of the single '1' character - works, but string conversion is unnecessary overhead when a simple shifting loop does the same job directly on the number itself.
Optimised: Since the number is guaranteed (after validation) to have exactly one set bit, repeatedly right-shifting it until it becomes 1 directly counts how many positions that bit was away from the rightmost position - a simple, direct simulation of 'sliding the single 1 down to the end'.
Scan every bit position and count
Check each of the 32 possible bit positions of n from least significant to most significant, testing whether that bit is set and keeping a running count of how many bits are set overall along with the position of the last set bit found. After scanning all positions, if the count of set bits is exactly one, print the recorded 1-indexed position; otherwise print -1. This directly follows the definition of 'exactly one set bit' without any bit tricks.
Time: O(log n) (at most 32 bit checks)
Space: O(1)
Validate with n & (n-1), then locate the bit
First check n > 0 and that (n & (n-1)) == 0; this classic bit trick clears the lowest set bit, so the result is zero exactly when n has zero or one set bits, letting us reject n = 0 and any number with more than one set bit in O(1). If the check passes, find the position of the single set bit by repeatedly right-shifting n and incrementing a counter until the shifted value becomes zero. This separates validation from localization, doing the expensive part only once we know exactly one bit is set.
Time: O(log n) to locate the bit after an O(1) validity check
Space: O(1)
Direct bit-position formula
Validate that n is a power of two using n > 0 and (n & (n-1)) == 0 as before, then compute the position directly as floor(log2(n)) + 1 using a constant-time bit-counting primitive (such as counting trailing zeros), rather than looping through bits one at a time. Because the position can be obtained in a single arithmetic/intrinsic operation instead of an iterative shift loop, this avoids the O(log n) scan entirely for the localization step. Print -1 immediately if the validity check fails.
Time: O(1)
Space: O(1)
Step by step
1. First confirm the number genuinely has exactly one set bit (using the n & (n-1) == 0 check from the 'power of 2' lesson) - if not, the answer is -1.
2. If it does, repeatedly shift the number right by one position, counting how many shifts it takes until the number becomes exactly 1.
3. That shift count, plus one (since we're counting positions starting from 1, not 0), is the bit's position.
Complexity
Time: Proportional to the position of the set bit (at most about 30 shifts for numbers up to a billion) - very fast.
Space: O(1) - just a counter variable.
Edge cases
Test n = 0 and a non-power-of-2 value (both must print -1), n = 1 (position 1), and n = 10^9 (largest input, not itself a power of 2).
Interview tip
Be ready to explain the validation step (n & (n-1) == 0, plus n != 0) BEFORE searching for the bit's position - skipping this check is a common way candidates miss the '-1 for invalid input' requirement entirely.