# Find position of rightmost set bit

Here given code implementation process.

``````// C Program
// Find position of rightmost set bit
#include <stdio.h>

#include <math.h>

// Finding the right most active bit position
void rightmostActiveBit(int n)
{
if (n <= 0)
{
return;
}
/*
Example
———————
n    = 320
n    = (00101000000)   Binary
-n   = (11011000000)   (2s)

Calculate log2
——————————————
Formula : log(n & -n) / log(2) + 1
-----------------------------------
log(320 & -320) / log(2) + 1)

Here : log(320 & -320) = log(64) = 4.158883
log(2)  = 0.693147

(4.158883 / 0.693147) + 1 = (7) position
————————————————————————————————————————
*/
// Calculate rightmost active bits
int result = (int)(log(n & -n) / log(2) + 1);
printf(" Number : %d Rightmost active : %d\n", n, result);
}
int main()
{
// Test Cases
// 320 = (101000000)
rightmostActiveBit(320);
// (1000) = (1111101000)
rightmostActiveBit(1000);
// (153) = (10011001)
rightmostActiveBit(153);
// (354) = (101100010)
rightmostActiveBit(354);
// 160 = (10100000)
rightmostActiveBit(160);
return 0;
}``````

#### Output

`````` Number : 320 Rightmost active : 7
Number : 1000 Rightmost active : 4
Number : 153 Rightmost active : 1
Number : 354 Rightmost active : 2
Number : 160 Rightmost active : 6``````
``````/*
Java Program for
Find position of rightmost set bit
*/
class BitPosition
{
// Finding the right most active bit position
public void rightmostActiveBit(int n)
{
if (n <= 0)
{
return;
}
/*
Example
———————
n    = 320
n    = (00101000000)   Binary
-n   = (11011000000)   (2s)

Calculate log2
——————————————
Formula : log(n & -n) / log(2) + 1
-----------------------------------
log(320 & -320) / log(2) + 1)

Here : log(320 & -320) = log(64) = 4.158883
log(2)  = 0.693147

(4.158883 / 0.693147) + 1 = (7) position
————————————————————————————————————————
*/
// Calculate rightmost active bits
int result = (int)(Math.log(n & -n) / Math.log(2) + 1);
System.out.print(" Number : " + n + " Rightmost active : " + result + "\n");
}
public static void main(String[] args)
{
// Test Cases
// 320 = (101000000)
// (1000) = (1111101000)
// (153) = (10011001)
// (354) = (101100010)
// 160 = (10100000)
}
}``````

#### Output

`````` Number : 320 Rightmost active : 7
Number : 1000 Rightmost active : 4
Number : 153 Rightmost active : 1
Number : 354 Rightmost active : 2
Number : 160 Rightmost active : 6``````
``````// Include header file
#include <iostream>

#include<math.h>

using namespace std;
/*
C++ Program for
Find position of rightmost set bit
*/
class BitPosition
{
public:
// Finding the right most active bit position
void rightmostActiveBit(int n)
{
if (n <= 0)
{
return;
}
/*
Example
———————
n    = 320
n    = (00101000000)   Binary
-n   = (11011000000)   (2s)

Calculate log2
——————————————
Formula : log(n &-n) / log(2) + 1
-----------------------------------
log(320 &-320) / log(2) + 1)

Here : log(320 &-320) = log(64) = 4.158883
log(2)  = 0.693147

(4.158883 / 0.693147) + 1 = (7) position
————————————————————————————————————————
*/
// Calculate rightmost active bits
int result = (int)(log(n & -n) / log(2) + 1);
cout << " Number : " << n << " Rightmost active : " << result << "\n";
}
};
int main()
{
// Test Cases
// 320 = (101000000)
// (1000) = (1111101000)
// (153) = (10011001)
// (354) = (101100010)
// 160 = (10100000)
return 0;
}``````

#### Output

`````` Number : 320 Rightmost active : 7
Number : 1000 Rightmost active : 4
Number : 153 Rightmost active : 1
Number : 354 Rightmost active : 2
Number : 160 Rightmost active : 6``````
``````// Include namespace system
using System;
/*
C# Program for
Find position of rightmost set bit
*/
public class BitPosition
{
// Finding the right most active bit position
public void rightmostActiveBit(int n)
{
if (n <= 0)
{
return;
}
/*
Example
———————
n    = 320
n    = (00101000000)   Binary
-n   = (11011000000)   (2s)

Calculate log2
——————————————
Formula : log(n & -n) / log(2) + 1
-----------------------------------
log(320 & -320) / log(2) + 1)

Here : log(320 & -320) = log(64) = 4.158883
log(2)  = 0.693147

(4.158883 / 0.693147) + 1 = (7) position
————————————————————————————————————————
*/
// Calculate rightmost active bits
int result = (int)(Math.Log(n & -n) / Math.Log(2) + 1);
Console.Write(" Number : " + n + " Rightmost active : " + result + "\n");
}
public static void Main(String[] args)
{
// Test Cases
// 320 = (101000000)
// (1000) = (1111101000)
// (153) = (10011001)
// (354) = (101100010)
// 160 = (10100000)
}
}``````

#### Output

`````` Number : 320 Rightmost active : 7
Number : 1000 Rightmost active : 4
Number : 153 Rightmost active : 1
Number : 354 Rightmost active : 2
Number : 160 Rightmost active : 6``````
``````<?php
/*
Php Program for
Find position of rightmost set bit
*/
class BitPosition
{
// Finding the right most active bit position
public	function rightmostActiveBit(\$n)
{
if (\$n <= 0)
{
return;
}
/*
Example
———————
n    = 320
n    = (00101000000)   Binary
-n   = (11011000000)   (2s)

Calculate log2
——————————————
Formula : log(n & -n) / log(2) + 1
-----------------------------------
log(320 & -320) / log(2) + 1)

Here : log(320 & -320) = log(64) = 4.158883
log(2)  = 0.693147

(4.158883 / 0.693147) + 1 = (7) position
————————————————————————————————————————
*/
// Calculate rightmost active bits
\$result = (int)(intval(log(\$n & -\$n) / log(2)) + 1);
echo " Number : ". \$n ." Rightmost active : ". \$result ."\n";
}
}

function main()
{
// Test Cases
// 320 = (101000000)
// (1000) = (1111101000)
// (153) = (10011001)
// (354) = (101100010)
// 160 = (10100000)
}
main();``````

#### Output

`````` Number : 320 Rightmost active : 7
Number : 1000 Rightmost active : 4
Number : 153 Rightmost active : 1
Number : 354 Rightmost active : 2
Number : 160 Rightmost active : 6``````
``````/*
Node Js Program for
Find position of rightmost set bit
*/
class BitPosition
{
// Finding the right most active bit position
rightmostActiveBit(n)
{
if (n <= 0)
{
return;
}
/*
Example
———————
n    = 320
n    = (00101000000)   Binary
-n   = (11011000000)   (2s)

Calculate log2
——————————————
Formula : log(n & -n) / log(2) + 1
-----------------------------------
log(320 & -320) / log(2) + 1)

Here : log(320 & -320) = log(64) = 4.158883
log(2)  = 0.693147

(4.158883 / 0.693147) + 1 = (7) position
————————————————————————————————————————
*/
// Calculate rightmost active bits
var result = parseInt((parseInt(Math.log(n & -n) / Math.log(2)) + 1));
process.stdout.write(" Number : " + n + " Rightmost active : " + result + "\n");
}
}

function main()
{
// Test Cases
// 320 = (101000000)
// (1000) = (1111101000)
// (153) = (10011001)
// (354) = (101100010)
// 160 = (10100000)
}
main();``````

#### Output

`````` Number : 320 Rightmost active : 7
Number : 1000 Rightmost active : 4
Number : 153 Rightmost active : 1
Number : 354 Rightmost active : 2
Number : 160 Rightmost active : 6``````
``````import math
#
#   Python 3 Program for
#   Find position of rightmost set bit

class BitPosition :
#  Finding the right most active bit position
def rightmostActiveBit(self, n) :
if (n <= 0) :
return

#
#     Example
#     ———————
#     n    = 320
#     n    = (00101000000)   Binary
#     -n   = (11011000000)   (2s)
#
#     Calculate log2
#     ——————————————
#     Formula : log(n & -n) / log(2) + 1
#     -----------------------------------
#     log(320 & -320) / log(2) + 1)
#
#     Here : log(320 & -320) = log(64) = 4.158883
#            log(2)  = 0.693147
#
#     (4.158883 / 0.693147) + 1 = (7) position
#     ————————————————————————————————————————

#  Calculate rightmost active bits
result = int((int(math.log(n & -n) / math.log(2)) + 1))
print(" Number : ", n ," Rightmost active : ", result )

def main() :
#  Test Cases
#  320 = (101000000)
#  (1000) = (1111101000)
#  (153) = (10011001)
#  (354) = (101100010)
#  160 = (10100000)

if __name__ == "__main__": main()``````

#### Output

`````` Number :  320  Rightmost active :  7
Number :  1000  Rightmost active :  4
Number :  153  Rightmost active :  1
Number :  354  Rightmost active :  2
Number :  160  Rightmost active :  6``````
``````#
#   Ruby Program for
#   Find position of rightmost set bit

class BitPosition
#  Finding the right most active bit position
def rightmostActiveBit(n)
if (n <= 0)
return
end

#
#     Example
#     ———————
#     n    = 320
#     n    = (00101000000)   Binary
#     -n   = (11011000000)   (2s)
#
#     Calculate log2
#     ——————————————
#     Formula : log(n & -n) / log(2) + 1
#     -----------------------------------
#     log(320 & -320) / log(2) + 1)
#
#     Here : log(320 & -320) = log(64) = 4.158883
#            log(2)  = 0.693147
#
#     (4.158883 / 0.693147) + 1 = (7) position
#     ————————————————————————————————————————

#  Calculate rightmost active bits
result = ((Math.log(n & -n) / Math.log(2) + 1)).to_i
print(" Number : ", n ," Rightmost active : ", result ,"\n")
end

end

def main()
#  Test Cases
#  320 = (101000000)
#  (1000) = (1111101000)
#  (153) = (10011001)
#  (354) = (101100010)
#  160 = (10100000)
end

main()``````

#### Output

`````` Number : 320 Rightmost active : 7
Number : 1000 Rightmost active : 4
Number : 153 Rightmost active : 1
Number : 354 Rightmost active : 2
Number : 160 Rightmost active : 6
``````
``````/*
Scala Program for
Find position of rightmost set bit
*/
class BitPosition
{
// Finding the right most active bit position
def rightmostActiveBit(n: Int): Unit = {
if (n <= 0)
{
return;
}
/*
Example
———————
n    = 320
n    = (00101000000)   Binary
-n   = (11011000000)   (2s)

Calculate log2
——————————————
Formula : log(n & -n) / log(2) + 1
-----------------------------------
log(320 & -320) / log(2) + 1)

Here : log(320 & -320) = log(64) = 4.158883
log(2)  = 0.693147

(4.158883 / 0.693147) + 1 = (7) position
————————————————————————————————————————
*/
// Calculate rightmost active bits
var result: Int = (((Math.log(n & -n) / Math.log(2)) + 1)).toInt;
print(" Number : " + n + " Rightmost active : " + result + "\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: BitPosition = new BitPosition();
// Test Cases
// 320 = (101000000)
// (1000) = (1111101000)
// (153) = (10011001)
// (354) = (101100010)
// 160 = (10100000)
}
}``````

#### Output

`````` Number : 320 Rightmost active : 7
Number : 1000 Rightmost active : 4
Number : 153 Rightmost active : 1
Number : 354 Rightmost active : 2
Number : 160 Rightmost active : 6``````
``````import Foundation
/*
Swift 4 Program for
Find position of rightmost set bit
*/
class BitPosition
{
// Finding the right most active bit position
func rightmostActiveBit(_ n: Int)
{
if (n <= 0)
{
return;
}
/*
Example
———————
n    = 320
n    = (00101000000)   Binary
-n   = (11011000000)   (2s)

Calculate log2
——————————————
Formula : log(n & -n) / log(2) + 1
-----------------------------------
log(320 & -320) / log(2) + 1)

Here : log(320 & -320) = log(64) = 4.158883
log(2)  = 0.693147

(4.158883 / 0.693147) + 1 = (7) position
————————————————————————————————————————
*/
// Calculate rightmost active bits
let result: Int = Int(log(Double(n & -n)) / log(2.0))+1
print(" Number : ", n ," Rightmost active : ", result );
}
}
func main()
{
// Test Cases
// 320 = (101000000)
// (1000) = (1111101000)
// (153) = (10011001)
// (354) = (101100010)
// 160 = (10100000)
}
main();``````

#### Output

`````` Number :  320  Rightmost active :  7
Number :  1000  Rightmost active :  4
Number :  153  Rightmost active :  1
Number :  354  Rightmost active :  2
Number :  160  Rightmost active :  6``````
``````/*
Kotlin Program for
Find position of rightmost set bit
*/
class BitPosition
{
// Finding the right most active bit position
fun rightmostActiveBit(n: Int): Unit
{
if (n <= 0)
{
return;
}
/*
Example
———————
n    = 320
n    = (00101000000)   Binary
-n   = (11011000000)   (2s)

Calculate log2
——————————————
Formula : log(n & -n) / log(2) + 1
-----------------------------------
log(320 & -320) / log(2) + 1)

Here : log(320 & -320) = log(64) = 4.158883
log(2)  = 0.693147

(4.158883 / 0.693147) + 1 = (7) position
————————————————————————————————————————
*/
// Calculate rightmost active bits
var result: Int = (Math.log((n and - n).toDouble()) / Math.log(2.0) + 1).toInt();
print(" Number : " + n + " Rightmost active : " + result + "\n");
}
}
fun main(args: Array < String > ): Unit
{
// Test Cases
// 320 = (101000000)
// (1000) = (1111101000)
// (153) = (10011001)
// (354) = (101100010)
// 160 = (10100000)
}``````

#### Output

`````` Number : 320 Rightmost active : 7
Number : 1000 Rightmost active : 4
Number : 153 Rightmost active : 1
Number : 354 Rightmost active : 2
Number : 160 Rightmost active : 6``````

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.