Position of rightmost set bit

Write a one line C 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 18(010010))
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 (10111)

2  Do an bit-wise & with original no, this will return no with the
required one only (00010)

3  Take the log2 of the no, you will get position -1 (1)

4  Add 1 (2)

Program:

C

#include<stdio.h>
#include<math.h>

unsigned int getFirstSetBitPos(int n)
{
   return log2(n&-n)+1;
}

int main()
{
    int n = 12;
    printf("%u", getFirstSetBitPos(n));
    getchar();
    return 0;
}

Java

// Java Code for Position of rightmost set bit
class 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 bit

import math

def getFirstSetBitPos(n):

     return math.log2(n&-n)+1

# driver code

n = 12
print(int(getFirstSetBitPos(n)))

# This code is contributed
# by Anant Agarwal.

C#

// C# Code for Position of rightmost set bit
using 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 bit

function getFirstSetBitPos($n)
{
    return ceil(log(($n& - 
                     $n) + 1, 2));
}

// Driver Code
$n = 12;
echo getFirstSetBitPos($n);
    
// This code is contributed by m_kit
?>


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 function
int main()
{ 
    int n = 12;
    cout<<getFirstSetBitPos(n)<<endl;
    return 0;
}

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 bit
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)) {

        // left shift
        m = m << 1;
        position++;
    }
    return position;
}
// Driver Code
int main()
{
    int n = 16;
    // function call
    cout << PositionRightmostSetbit(n);
    return 0;
}

Java

// Java program to find the 
// first rightmost set bit 
// using XOR operator

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
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 bit
def 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 Code
n = 16

# function call
print(PositionRightmostSetbit(n))

# This code is contributed 
# by Smitha


C#

// C# program to find the 
// first rightmost set bit 
// using XOR operator
using 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 bit
function 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 call
echo PositionRightmostSetbit($n);
    
// This code is contributed by ajit
?>

Output:

5

This approach has been contributed by mubashshir ahmad



My Personal Notes arrow_drop_up


 
Practice Tags :
Article Tags :

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



2.6 Average Difficulty : 2.6/5.0
Based on 81 vote(s)






User Actions