Position of rightmost set bit (original) (raw)

Last Updated : 5 Feb, 2026

Given an integer **n, Return the position of the **first set bit from right to left in the binary representation n. If no set bits present , then return 0.
**Note: Position of rightmost bit is 1.

**Examples:

**Input: n = 18
**Output: 2
**Explanation: Binary representation of 18 is 10010, hence position of first set bit from right is 2.

**Input: n = 19
**Output: 1
**Explanation: Binary representation of 19 is 10011, hence position of first set bit from right is 1.

Try It Yourselfredirect icon

Table of Content

Using 2's complement and log Operator - O(log n) time and O(1) space

The idea is to find the rightmost set bit using a property of 2's complement arithmetic. When we perform n & (~n + 1) or equivalently n & (-n), we get a number with only the rightmost set bit of n. Then we can use log2 to find its position.

C++ `

#include #include

using namespace std;

int getFirstSetBit(int n) {

// If no set bit 
if (n == 0) return 0;

// Get the rightmost set bit
int res = n & (~n + 1);

// Find position using log2
return log2(res) + 1;

}

int main() { int n = 18; cout << getFirstSetBit(n); return 0; }

Java

import java.lang.Math;

class GfG {

static int getFirstSetBit(int n) {
    
    // If no set bit 
    if (n == 0) return 0;
    
    // Get the rightmost set bit
    int res = n & (~n + 1);
    
    // Find position using log2
    return (int)(Math.log(res) / Math.log(2)) + 1;
}

public static void main(String[] args) {
    int n = 18;
    System.out.println(getFirstSetBit(n));
}

}

Python

import math def getFirstSetBit(n):

# If no set bit 
if n == 0:
    return 0

# Get the rightmost set bit
res = n & (~n + 1)

# Find position using log2
return int(math.log2(res)) + 1

if name == "main": n = 18 print(getFirstSetBit(n))

C#

using System;

class GfG {

static uint getFirstSetBit(int n) {
    
    // If no set bit 
    if (n == 0) return 0;
    
    // Get the rightmost set bit
    int res = n & (~n + 1);
    
    // Find position using log2
    return (uint)(Math.Log(res) / Math.Log(2)) + 1;
}

static void Main() {
    int n = 18;
    Console.WriteLine(getFirstSetBit(n));
}

}

JavaScript

function getFirstSetBit(n) {

// If no set bit 
if (n === 0) return 0;

// Get the rightmost set bit
let res = n & (~n + 1);

// Find position using log2
return Math.log2(res) + 1;

} //Driver Code let n = 18; console.log(getFirstSetBit(n));

`

Using left shift operator - O(1) time and O(1) space

The idea is to use a counter and iteratively left-shift 1 to check each bit position. Increment a position counter until we find a bit that is set.

C++ `

#include using namespace std;

int getFirstSetBit(int n) {

// If no set bit
if (n == 0) return 0;

int pos = 1;

// Left shift a mask and check each bit
while ((n & (1 << (pos - 1))) == 0) {
    pos++;
}

return pos;

}

int main() { int n = 18; cout << getFirstSetBit(n); return 0; }

Java

class GFG {

static int getFirstSetBit(int n) {

    // If no set bit
    if (n == 0) return 0;

    int pos = 1;

    // Left shift a mask and check each bit
    while ((n & (1 << (pos - 1))) == 0) {
        pos++;
    }

    return pos;
}

public static void main(String[] args) {
    int n = 18;
    System.out.println(getFirstSetBit(n));
}

}

Python

def getFirstSetBit(n):

# If no set bit
if n == 0:
    return 0

pos = 1

# Left shift a mask and check each bit
while (n & (1 << (pos - 1))) == 0:
    pos += 1

return pos

n = 18 print(getFirstSetBit(n))

C#

using System;

class GFG { static int getFirstSetBit(int n) {

    // If no set bit
    if (n == 0) return 0;

    int pos = 1;

    // Left shift a mask and check each bit
    while ((n & (1 << (pos - 1))) == 0) {
        pos++;
    }

    return pos;


static void Main() {
    int n = 18;
    Console.WriteLine(getFirstSetBit(n));
}

}

JavaScript

function getFirstSetBit(n) {

// If no set bit
if (n === 0) return 0;

let pos = 1;

// Left shift a mask and check each bit
while ((n & (1 << (pos - 1))) === 0) {
    pos++;
}

return pos;

}

let n = 18; console.log(getFirstSetBit(n));

`

Using right shift operator - O(1) time and O(1) space

The idea is to right-shift the number and check the least significant bit in each iteration. Increment a position counter until we find a bit that is set.

C++ `

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

int getFirstSetBit(int n) {

// If no set bit 
if (n == 0) return 0;

int res = 1;

// Right shift and check LSB
while (!(n & 1)) {
    n = n >> 1;
    res++;
}

return res;

}

int main() { int n = 18; cout << getFirstSetBit(n); return 0; }

Java

// Java program to find the position of right most // set bit in an integer.

class GfG {

static int getFirstSetBit(int n) {
    
    // If no set bit 
    if (n == 0) return 0;
    
    int res = 1;
    
    // Right shift and check LSB
    while ((n & 1) == 0) {
        n = n >> 1;
        res++;
    }
    
    return res;
}

public static void main(String[] args) {
    int n = 18;
    System.out.println(getFirstSetBit(n));
}

}

Python

def getFirstSetBit(n):

# If no set bit 
if n == 0:
    return 0

res = 1

# Right shift and check LSB
while (n & 1) == 0:
    n = n >> 1
    res += 1

return res

if name == "main": n = 18 print(getFirstSetBit(n))

C#

using System;

class GfG {

static uint getFirstSetBit(int n) {
    
    // If no set bit 
    if (n == 0) return 0;
    
    uint res = 1;
    
    // Right shift and check LSB
    while ((n & 1) == 0) {
        n = n >> 1;
        res++;
    }
    
    return res;
}

static void Main() {
    int n = 18;
    Console.WriteLine(getFirstSetBit(n));
}

}

JavaScript

function getFirstSetBit(n) {

// If no set bit 
if (n === 0) return 0;

let res = 1;

// Right shift and check LSB
while ((n & 1) === 0) {
    n = n >> 1;
    res++;
}

return res;

}

let n = 18; console.log(getFirstSetBit(n));

`

Using Built-In Library Functions - O(1) time and O(1) space

C++ `

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

int getFirstSetBit(int n) {

// If no set bit 
if (n == 0) return 0;

return __builtin_ffs(n);

}

int main() { int n = 18; cout << getFirstSetBit(n); return 0; }

Java

// Java program to find the position of right most // set bit in an integer.

class GfG {

static int getFirstSetBit(int n) {
    
    // If no set bit 
    if (n == 0) return 0;
    
    return Integer.numberOfTrailingZeros(n) + 1;
}

public static void main(String[] args) {
    int n = 18;
    System.out.println(getFirstSetBit(n));
}

}

Python

Python program to find the position of right most

set bit in an integer.

def getFirstSetBit(n):

# If no set bit 
if n == 0:
    return 0

return (n & -n).bit_length()

if name == "main": n = 18 print(getFirstSetBit(n))

`