Position of the only set bit

Last Updated : 22 Apr, 2026

Given a number n containing only 1 set bit in its binary representation, the task is to find the position of the only set bit. If there are 0 or more than 1 set bits, then return -1.

Note: Position of set bit '1' should be counted starting with 1 from the LSB side in the binary representation of the number.

Examples:-

Input: n = 2
Output: 2
Explanation: Binary representation of 2 is 10. We can observe that the only set bit is at position 2 from LSB.

Input: n = 5
Output: -1
Explanation: Binary representation of 5 is 101. There are 2 set bits, so return -1.

Try It Yourself
redirect icon

Condition for numbers having only 1 set bit:

Numbers having only one set bit will be a power of 2 number ( example, 20 = 1, 21 = 10, 22 = 100, 23 = 1000).

When you subtract 1 from such a number, all bits after the set bit (including the set bit itself) flip (e.g., 4 = 100, 3 = 011). Performing a bitwise AND between n and n-1 results in 0 if n is a power of 2, as the single set bit cancels out. 

Using Left Shift Operator - O(log(n)) time and O(1) space

The idea is to use a loop where we left shift the number 1 and perform a bitwise AND operation with n. If the result is non-zero, the position of the set bit is determined by the number of shifts performed.

Dry run for n = 2 (binary = 10) :

  • Check (n == 0 => false) OR ((n & (n - 1)) != 0)
    (n & (n - 1)) = (2 & 1) = 0 => (0 != 0) = false
    So (false OR false = false), hence continue.
  • Initialize pos = 1, val = 1 (binary: 01)
  • (val & n) = (1 & 2) = 0 so condition true, enter loop
    val = val << 1 = 1 << 1 = 2 (binary: 10), pos = 2
  • (val & n) = (2 & 2) = 2 so condition false, exit loop

Final answer is: pos = 2

C++
#include <bits/stdc++.h>
using namespace std;

int findPosition(int n)
{
    // Check if n has exactly one set bit
    if (n == 0 || (n & (n - 1)) != 0)
        return -1;

    int pos = 1;
    int val = 1;
    while ((val & n) == 0)
    {
        // shifting left
        val = val << 1;
        pos++;
    }
    return pos;
}

// Main function
int main()
{
    int n = 2;
    cout << findPosition(n);
    return 0;
}
Java
class GfG {

    static int findPosition(int n)
    {
        // Check if n has exactly one set bit
        if (n == 0 || (n & (n - 1)) != 0)
            return -1;

        int pos = 1;
        int val = 1;
        while ((val & n) == 0) {
            // Shifting left
            val = val << 1;
            pos++;
        }
        return pos;
    }

    // Main function
    public static void main(String[] args)
    {
        int n = 2;
        System.out.println(findPosition(n));
    }
}
Python
def findPosition(n):
    
    # Check if n has exactly one set bit
    if n == 0 or (n & (n - 1)) != 0:
        return -1
    
    pos = 1
    val = 1
    while (val & n) == 0:
        # Shifting left
        val = val << 1
        pos += 1
    return pos

# Driver Code
if __name__ == "__main__":
    n = 2
    print(findPosition(n))
C#
using System;

class GfG {

    static int findPosition(int n) {
        
        // Check if n has exactly one set bit
        if (n == 0 || (n & (n - 1)) != 0) return -1; 
        
        int pos = 1;
        int val = 1;
        while ((val & n) == 0) {
            // shifting left
            val = val << 1;
            pos++;
        }
        return pos;
    }

    // Main function
    static void Main() {
        int n = 2;
        Console.WriteLine(findPosition(n));
    }
}
JavaScript
function findPosition(n) {
    
    // Check if n has exactly one set bit
    if (n === 0 || (n & (n - 1)) !== 0) return -1; 
    
    let pos = 1;
    let val = 1;
    while ((val & n) === 0) {
        // Shifting left
        val = val << 1;
        pos++;
    }
    return pos;
}

// Driver Code
let n = 2;
console.log(findPosition(n));

Output
2

Using Right Shift Operator - O(log(n)) time and O(1) space

The idea is to right shift the number n until the rightmost bit becomes 1. The number of shifts required to reach this point gives the position of the set bit.

Dry run for n = 2 (binary = 10) :

  • Check (n == 0 => false) OR ((n & (n - 1)) != 0)
    (n & (n - 1)) = (2 & 1) = 0 => (0 != 0) = false
    So (false OR false = false), hence continue.
  • Initialize pos = 1
  • (n & 1) = (2 & 1) = 0 so condition is true, enter loop
    n = n >> 1 = 2 >> 1 = 1 (binary: 01), pos = 2
  • (n & 1) = (1 & 1) = 1 so condition is false, exit loop

Final answer is: pos = 2

C++
#include <bits/stdc++.h>
using namespace std;

int findPosition(int n)
{
    // Check if n has exactly one set bit
    if (n == 0 || (n & (n - 1)) != 0)
        return -1;

    int pos = 1;
    while ((n & 1) == 0)
    {
        // Shifting right
        n = n >> 1;
        pos++;
    }
    return pos;
}

// Main function
int main()
{
    int n = 2;
    cout << findPosition(n);
    return 0;
}
Java
class GfG {

    static int findPosition(int n)
    {
        // Check if n has exactly one set bit
        if (n == 0 || (n & (n - 1)) != 0)
            return -1;

        int pos = 1;
        while ((n & 1) == 0) {
            
            // Shifting right
            n = n >> 1;
            pos++;
        }
        return pos;
    }

    // Main function
    public static void main(String[] args)
    {
        int n = 2;
        System.out.println(findPosition(n));
    }
}
Python
def findPosition(n):

    # Check if n has exactly one set bit
    if n == 0 or (n & (n - 1)) != 0:
        return -1

    pos = 1
    while (n & 1) == 0:

        # Shifting right
        n = n >> 1
        pos += 1
    return pos

# Driver code
if __name__ == "__main__":
    n = 2
    print(findPosition(n))
C#
using System;

class GfG {

    static int findPosition(int n)
    {
        // Check if n has exactly one set bit
        if (n == 0 || (n & (n - 1)) != 0)
            return -1;

        int pos = 1;
        while ((n & 1) == 0) {

            // Shifting right
            n = n >> 1;
            pos++;
        }
        return pos;
    }

    // Main function
    static void Main()
    {
        int n = 2;
        Console.WriteLine(findPosition(n));
    }
}
JavaScript
function findPosition(n)
{
    // Check if n has exactly one set bit
    if (n === 0 || (n & (n - 1)) !== 0)
        return -1;

    let pos = 1;
    while ((n & 1) === 0) {

        // Shifting right
        n = n >> 1;
        pos++;
    }
    return pos;
}

// Driver code
let n = 2;
console.log(findPosition(n));

Output
2

Using Log Operator - O(log n) time and O(1) space

The idea is to use the mathematical property that the position of the only set bit in a number n (which is a power of 2) can be found by taking the base-2 logarithm of n and adding 1 (since the position is 1-based).

C++
#include <bits/stdc++.h>
using namespace std;

int findPosition(int n)
{
    // Check if n has exactly one set bit
    if (n == 0 || (n & (n - 1)) != 0)
        return -1;

    // Find position of the set bit using log2
    return log2(n) + 1;
}

// Main function
int main()
{
    int n = 2;
    cout << findPosition(n);
    return 0;
}
Java
class GfG {

    static int findPosition(int n)
    {
        // Check if n has exactly one set bit
        if (n == 0 || (n & (n - 1)) != 0)
            return -1;

        // Find position of the set bit using log2
        return (int)(Math.log(n) / Math.log(2)) + 1;
    }

    // Main function
    public static void main(String[] args)
    {
        int n = 2;
        System.out.println(findPosition(n));
    }
}
Python
import math

def findPosition(n):

    # Check if n has exactly one set bit
    if n == 0 or (n & (n - 1)) != 0:
        return -1

    # Find position of the set bit using log2
    return int(math.log2(n)) + 1


# Driver code
if __name__ == "__main__":
    n = 2
    print(int(findPosition(n)))
C#
using System;

class GfG {

    static int findPosition(int n)
    {
        // Check if n has exactly one set bit
        if (n == 0 || (n & (n - 1)) != 0)
            return -1;

        // Find position of the set bit using log2
        return (int)(Math.Log(n) / Math.Log(2)) + 1;
    }

    // Main function
    static void Main()
    {
        int n = 2;
        Console.WriteLine(findPosition(n));
    }
}
JavaScript
function findPosition(n) {
    
    // Check if n has exactly one set bit
    if (n === 0 || (n & (n - 1)) !== 0) return -1; 
    
    // Find position of the set bit using log2
    return Math.log2(n) + 1;
}

// Driver code
let n = 2;
console.log(Math.floor(findPosition(n)));

Output
2

Related Article:

Comment