Write a one line function to return position of first 1 from right to left, in binary representation of an Integer.
I/P 18, Binary Representation 010010 O/P 2 I/P 19, Binary Representation 010011 O/P 1
Algorithm: (Example 12(1100)) Let I/P be 12 (1100) 1. Take two's complement of the given no as all bits are reverted except the first '1' from right to left (0100) 2 Do a bit-wise & with original no, this will return no with the required one only (0100) 3 Take the log2 of the no, you will get (position - 1) (2) 4 Add 1 (3)
Program:
C++
// C++ program for Position// of rightmost set bit#include <iostream>#include <math.h>using namespace std;class gfg{public:unsigned int getFirstSetBitPos(int n){ return log2(n & -n) + 1;}};// Driver codeint main(){ gfg g; int n = 12; cout << g.getFirstSetBitPos(n); return 0;}// This code is contributed by SoM15242 |
C
// C program for Position// of rightmost set bit#include <math.h>#include <stdio.h>unsigned int getFirstSetBitPos(int n){ return log2(n & -n) + 1;}// Driver codeint main(){ int n = 12; printf("%u", getFirstSetBitPos(n)); getchar(); return 0;} |
Java
// Java Code for Position of rightmost set bitclass GFG { public static int getFirstSetBitPos(int n) { return (int)((Math.log10(n & -n)) / Math.log10(2)) + 1; } // Drive code public static void main(String[] args) { int n = 12; System.out.println(getFirstSetBitPos(n)); }}// This code is contributed by Arnav Kr. Mandal |
Python3
# Python Code for Position# of rightmost set bitimport mathdef getFirstSetBitPos(n): return math.log2(n&-n)+1# driver coden = 12print(int(getFirstSetBitPos(n)))# This code is contributed# by Anant Agarwal. |
C#
// C# Code for Position of rightmost set bitusing System;class GFG { public static int getFirstSetBitPos(int n) { return (int)((Math.Log10(n & -n)) / Math.Log10(2)) + 1; } // Driver method public static void Main() { int n = 12; Console.WriteLine(getFirstSetBitPos(n)); }}// This code is contributed by Sam007 |
PHP
<?php// PHP Code for Position of// rightmost set bitfunction getFirstSetBitPos($n){ return ceil(log(($n& - $n) + 1, 2));}// Driver Code$n = 12;echo getFirstSetBitPos($n); // This code is contributed by m_kit?> |
3
Output:
3
Using ffs() function: ffs() function returns the index of first least significant set bit. The indexing starts in ffs() function from 1.
For example:
n = 12 = 1100
In above example, ffs(n) returns the rightmost set bit index which is 3.
C++
// C++ program to find the// position of first rightmost// set bit in a given number.#include <bits/stdc++.h>using namespace std;// Function to find rightmost// set bit in given number.int getFirstSetBitPos(int n){ return ffs(n);}// Driver functionint main(){ int n = 12; cout << getFirstSetBitPos(n) << endl; return 0;} |
3
Using XOR and & operator :
Initialize m as 1 as check its XOR with the bits starting from the rightmost bit. Left shift m by one till we find the first set bit, as the first set bit gives a number when we perform a & operation with m.
C++
// C++ program to find the first// rightmost set bit using XOR operator#include <bits/stdc++.h>using namespace std;// function to find the rightmost set bitint PositionRightmostSetbit(int n){ // Position variable initialize with 1 // m variable is used to check the set bit int position = 1; int m = 1; while (!(n & m)) { // left shift m = m << 1; position++; } return position;}// Driver Codeint main(){ int n = 16; // function call cout << PositionRightmostSetbit(n); return 0;} |
Java
// Java program to find the// first rightmost set bit// using XOR operatorclass GFG { // function to find // the rightmost set bit static int PositionRightmostSetbit(int n) { // Position variable initialize // with 1 m variable is used to // check the set bit int position = 1; int m = 1; while ((n & m) == 0) { // left shift m = m << 1; position++; } return position; } // Driver Code public static void main(String[] args) { int n = 16; // function call System.out.println(PositionRightmostSetbit(n)); }}// This code is contributed// by Smitha |
Python3
# Python3 program to find # the first rightmost set # bit using XOR operator# function to find the # rightmost set bitdef PositionRightmostSetbit(n): # Position variable initialize # with 1 m variable is used # to check the set bit position = 1 m = 1 while (not(n & m)) : # left shift m = m << 1 position += 1 return position# Driver Coden = 16# function callprint(PositionRightmostSetbit(n))# This code is contributed # by Smitha |
C#
// C# program to find the// first rightmost set bit// using XOR operatorusing System;class GFG { // function to find // the rightmost set bit static int PositionRightmostSetbit(int n) { // Position variable initialize // with 1 m variable is used to // check the set bit int position = 1; int m = 1; while ((n & m) == 0) { // left shift m = m << 1; position++; } return position; } // Driver Code static public void Main() { int n = 16; // function call Console.WriteLine( PositionRightmostSetbit(n)); }}// This code is contributed// by @ajit |
PHP
<?php// PHP program to find the // first rightmost set bit// using XOR operator// function to find the // rightmost set bitfunction PositionRightmostSetbit($n){ // Position variable initialize // with 1 m variable is used to // check the set bit $position = 1; $m = 1; while (!($n & $m)) { // left shift $m = $m << 1; $position++; } return $position;}// Driver Code$n = 16;// function callecho PositionRightmostSetbit($n); // This code is contributed by ajit?> |
5
This approach has been contributed by mubashshir ahmad
Using Left Shift (<<) : Initialize pos with 1, iterate up to INT_SIZE(Here 32) and check whether bit is set or not, if bit is set then break the loop, else increment the pos.
C++
// C++ implementation of above approach#include <iostream>using namespace std;#define INT_SIZE 32int Right_most_setbit(int num){ int pos = 1; // counting the position of first set bit for (int i = 0; i < INT_SIZE; i++) { if (!(num & (1 << i))) pos++; else break; } return pos;}int main(){ int num = 18; int pos = Right_most_setbit(num); cout << pos << endl; return 0;}// This approach has been contributed by @vivek kumar9 |
Java
//Java implementation of above approachpublic class GFG { static int INT_SIZE = 32; static int Right_most_setbit(int num) { int pos = 1; // counting the position of first set bit for (int i = 0; i < INT_SIZE; i++) { if ((num & (1 << i))== 0) pos++; else break; } return pos; } //Driver code public static void main(String[] args) { int num = 18; int pos = Right_most_setbit(num); System.out.println(pos); }} |
Python3
# Python 3 implementation of above approachINT_SIZE = 32def Right_most_setbit(num) : pos = 1 # counting the position of first set bit for i in range(INT_SIZE) : if not(num & (1 << i)) : pos += 1 else : break return pos if __name__ == "__main__" : num = 18 pos = Right_most_setbit(num) print(pos) # This code is contributed by ANKITRAI1 |
C#
// C# implementation of above approachusing System;class GFG { static int INT_SIZE = 32; static int Right_most_setbit(int num) { int pos = 1; // counting the position // of first set bit for (int i = 0; i < INT_SIZE; i++) { if ((num & (1 << i))== 0) pos++; else break; } return pos; } // Driver code static public void Main () { int num = 18; int pos = Right_most_setbit(num); Console.WriteLine(pos); } } |
PHP
<?php// PHP implementation of above approachfunction Right_most_setbit($num){ $pos = 1; $INT_SIZE = 32; // counting the position // of first set bit for ($i = 0; $i < $INT_SIZE; $i++) { if (!($num & (1 << $i))) $pos++; else break; } return $pos;}// Driver code$num = 18;$pos = Right_most_setbit($num);echo $pos;echo ("\n")// This code is contributed // by Shivi_Aggarwal?> |
2
Output :
2
Another Method Using right Shift(>>):
Initialize pos=1 . Iterate till number>0, at each step check if the last bit is set. If last bit is set , return current position, else increment pos by 1 and right shift n by 1.
C++
//CPP program for above approach #include<bits/stdc++.h>using namespace std;// Program to find position of// rightmost set bitint PositionRightmostSetbit(int n){ int p=1; // Iterate till number>0 while(n > 0) { // Checking if last bit is set if(n&1){ return p; } // Increment position and right shift number p++; n=n>>1; } // set bit not found. return -1;}// Driver Codeint main() { int n=18; // Function call int pos=Last_set_bit(n); if(pos!=-1) cout<<pos; else cout<<0; return 0;} |
2
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Get the position of rightmost unset bit
- Position of rightmost common bit in two numbers
- Position of rightmost bit with first carry in sum of two binary
- Position of rightmost different bit
- Turn off the rightmost set bit | Set 2
- Set the rightmost unset bit
- Set the rightmost unset bit
- Set the rightmost off bit
- Number formed by the rightmost set bit in N
- Number formed by flipping all bits to the left of rightmost set bit
- Turn off the rightmost set bit
- Find position of the only set bit
- Position of the K-th set bit in a number
- Check whether the bit at given position is set or unset
- Inserting M into N such that m starts at bit j and ends at bit i | Set-2
- Minimum bit flips such that every K consecutive bits contain at least one set bit
- Print leftmost and rightmost nodes of a Binary Tree
- Modify a bit at a given position
- Check whether the two numbers differ at one bit position only
- Find position of left most dis-similar bit for two numbers

