Position of rightmost set bit
Given an integer n, the task is to return the position of the first set bit from right to left in the binary representation n. If n is equal to 0 (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.
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++ program to find the position of right most
// set bit in an integer.
#include <bits/stdc++.h>
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 program to find the position of right most
// set bit in an integer.
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 program to find the position of right most
# set bit in an integer.
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# program to find the position of right most
// set bit in an integer.
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 program to find the position of right most
// set bit in an integer.
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;
}
let n = 18;
console.log(getFirstSetBit(n));
Output
2
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++ program to find the res of right most
// set bit in an integer.
#include <bits/stdc++.h>
using namespace std;
int getFirstSetBit(int n) {
// If no set bit
if (n == 0) return 0;
int res = 1;
// Left shift 1 and check each bit
while ((n & 1) == 0) {
n = n >> 1;
res++;
}
return res;
}
int main() {
int n = 18;
cout << getFirstSetBit(n);
return 0;
}
// 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;
// Left shift 1 and check each bit
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 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
res = 1
# Left shift 1 and check each bit
while (n & 1) == 0:
n = n >> 1
res += 1
return res
if __name__ == "__main__":
n = 18
print(getFirstSetBit(n))
// C# program to find the position of right most
// set bit in an integer.
using System;
class GfG {
static uint getFirstSetBit(int n) {
// If no set bit
if (n == 0) return 0;
uint res = 1;
// Left shift 1 and check each bit
while ((n & 1) == 0) {
n = n >> 1;
res++;
}
return res;
}
static void Main() {
int n = 18;
Console.WriteLine(getFirstSetBit(n));
}
}
// JavaScript program to find the position of right most
// set bit in an integer.
function getFirstSetBit(n) {
// If no set bit
if (n === 0) return 0;
let res = 1;
// Left shift 1 and check each bit
while ((n & 1) === 0) {
n = n >> 1;
res++;
}
return res;
}
let n = 18;
console.log(getFirstSetBit(n));
Output
2
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++ program to find the position of right most
// set bit in an integer.
#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 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 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
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# program to find the position of right most
// set bit in an integer.
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 program to find the position of right most
// set bit in an integer.
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));
Output
2
Using Built-In Library Functions - O(1) time and O(1) space
// C++ program to find the position of right most
// set bit in an integer.
#include <bits/stdc++.h>
using namespace std;
int getFirstSetBit(int n) {
// If no set bit
if (n == 0) return 0;
return ffs(n);
}
int main() {
int n = 18;
cout << getFirstSetBit(n);
return 0;
}
// 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 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))
Output
2