Position of rightmost common bit in two numbers
Given two non-negative numbers m and n. Find the position of rightmost same bit in the binary representation of the numbers.
Examples:
Input : m = 10, n = 9
Output : 3
(10)10 = (1010)2
(9)10 = (1001)2
It can be seen that the 3rd bit
from the right is same.
Input : m = 16, n = 7
Output : 4
(16)10 = (10000)2
(7)10 = (111)2, can also be written as
= (00111)2
It can be seen that the 4th bit
from the right is same.
Approach: Get the bitwise xor of m and n. Let it be xor_value = m ^ n. Now, get the position of rightmost unset bit in xor_value.
Explanation: The bitwise xor operation produces a number which has unset bits only at the positions where the bits of m and n are same. Thus, the position of rightmost unset bit in xor_value gives the position of rightmost same bit.
C++
#include <bits/stdc++.h>
using namespace std;
int getRightMostSetBit(unsigned int n)
{
return log2(n & -n) + 1;
}
int posOfRightMostSameBit(unsigned int m,
unsigned int n)
{
return getRightMostSetBit(~(m ^ n));
}
int main()
{
int m = 16, n = 7;
cout << "Position = "
<< posOfRightMostSameBit(m, n);
return 0;
}
|
Java
class GFG {
static int getRightMostSetBit( int n)
{
return ( int )((Math.log(n & -n))/(Math.log( 2 )))
+ 1 ;
}
static int posOfRightMostSameBit( int m, int n)
{
return getRightMostSetBit(~(m ^ n));
}
public static void main (String[] args)
{
int m = 16 , n = 7 ;
System.out.print( "Position = "
+ posOfRightMostSameBit(m, n));
}
}
|
Python3
import math
def getRightMostSetBit(n):
return int (math.log2(n & - n)) + 1
def posOfRightMostSameBit(m, n):
return getRightMostSetBit(~(m ^ n))
m, n = 16 , 7
print ( "Position = " , posOfRightMostSameBit(m, n))
|
C#
using System;
class GFG
{
static int getRightMostSetBit( int n)
{
return ( int )((Math.Log(n & -n)) / (Math.Log(2))) + 1;
}
static int posOfRightMostSameBit( int m, int n)
{
return getRightMostSetBit(~(m ^ n));
}
public static void Main ()
{
int m = 16, n = 7;
Console.Write( "Position = "
+ posOfRightMostSameBit(m, n));
}
}
|
PHP
<?php
function getRightMostSetBit( $n )
{
return log( $n & - $n ) + 1;
}
function posOfRightMostSameBit( $m ,
$n )
{
return getRightMostSetBit(~( $m ^ $n ));
}
$m = 16; $n = 7;
echo "Position = "
, ceil (posOfRightMostSameBit( $m , $n ));
?>
|
Javascript
<script>
function getRightMostSetBit(n)
{
return Math.log2(n & -n) + 1;
}
function posOfRightMostSameBit(m, n)
{
return getRightMostSetBit(~(m ^ n));
}
let m = 16, n = 7;
document.write( "Position = "
+ posOfRightMostSameBit(m, n));
</script>
|
Output:
Position = 4
Time Complexity: O(1)
Auxiliary Space: O(1)
Alternate Approach: Until both the value becomes zero, check last bits of both numbers and right shift. At any moment, both bits are same, return counter.
Explanation: Rightmost bit of two values m and n are equal only when both values are either odd or even.
C++
#include <iostream>
using namespace std;
static int posOfRightMostSameBit( int m, int n)
{
int loopCounter = 1;
while (m > 0 || n > 0)
{
bool a = m % 2 == 1;
bool b = n % 2 == 1;
if (!(a ^ b))
{
return loopCounter;
}
m = m >> 1;
n = n >> 1;
loopCounter++;
}
return -1;
}
int main()
{
int m = 16, n = 7;
cout << "Position = "
<< posOfRightMostSameBit(m, n);
}
|
Java
class GFG {
static int posOfRightMostSameBit( int m, int n)
{
int loopCounter = 1 ;
while (m > 0 || n > 0 ){
boolean a = m% 2 == 1 ;
boolean b = n% 2 == 1 ;
if (!(a ^ b)){
return loopCounter;}
m = m >> 1 ;
n = n >> 1 ;
loopCounter++;
}
return - 1 ;
}
public static void main (String[] args)
{
int m = 16 , n = 7 ;
System.out.print( "Position = "
+ posOfRightMostSameBit(m, n));
}
}
|
Python3
def posOfRightMostSameBit(m, n):
loopCounter = 1
while (m > 0 or n > 0 ):
a = m % 2 = = 1
b = n % 2 = = 1
if ( not (a ^ b)):
return loopCounter
m = m >> 1
n = n >> 1
loopCounter + = 1
return - 1
if __name__ = = '__main__' :
m, n = 16 , 7
print ( "Position = " ,
posOfRightMostSameBit(m, n))
|
C#
using System;
class GFG
{
static int posOfRightMostSameBit( int m, int n)
{
int loopCounter = 1;
while (m > 0 || n > 0)
{
Boolean a = m % 2 == 1;
Boolean b = n % 2 == 1;
if (!(a ^ b))
{
return loopCounter;
}
m = m >> 1;
n = n >> 1;
loopCounter++;
}
return -1;
}
public static void Main (String[] args)
{
int m = 16, n = 7;
Console.Write( "Position = "
+ posOfRightMostSameBit(m, n));
}
}
|
Javascript
<script>
function posOfRightMostSameBit(m, n)
{
let loopCounter = 1;
while (m > 0 || n > 0)
{
let a = m % 2 == 1;
let b = n % 2 == 1;
if (!(a ^ b))
{
return loopCounter;
}
m = m >> 1;
n = n >> 1;
loopCounter++;
}
return -1;
}
let m = 16, n = 7;
document.write( "Position = "
+ posOfRightMostSameBit(m, n));
</script>
|
Output:
Position = 4
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
21 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...