Given two numbers m and n. Find the position of the rightmost different bit in the binary representation of numbers. It is guaranteed that such a bit exists
Examples:
Input: m = 11, n = 9
Output: 2
Explanation:
(11)10 = (1011)2
(9)10 = (1001)2
It can be seen that 2nd bit from the right is different
Input: m = 52, n = 4
Output: 5
Explanation:
(52)10 = (110100)2
(4)10 = (100)2, can also be written as = (000100)2
It can be seen that 5th bit from the right is different
Position of rightmost different bit using XOR:
Get the bitwise xor of m and n. Let it be xor_value = m ^ n. Now, find the position of rightmost set bit in xor_value. As 0 XOR 1 and 1 XOR 0 equals 1, so if a bit is set in the XOR value then it means that the bits at that position were different in the given numbers
An efficient way to find the rightmost set bit:
log2(n & -n) + 1 gives us the position of the rightmost set bit.
(-n) reverses all the bits from left to right till the last set bit
for example: n = 16810
binary signed 2’s complement of n = 00000000101010002
binary signed 2’s complement of -n = 11111111010110002
? (n & -n) = 00000000000010002 = 8
now, log2(n & -n) = log2(8) = 3
log2(n & -n) + 1 = 4 (position of rightmost set bit)
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int getRightMostSetBit( int n)
{
if (n == 0)
return 0;
return log2(n & -n) + 1;
}
int posOfRightMostDiffBit( int m, int n)
{
return getRightMostSetBit(m ^ n);
}
int main()
{
int m = 52, n = 24;
cout << "Position of rightmost different bit:"
<< posOfRightMostDiffBit(m, n) << endl;
return 0;
}
|
Java
class GFG {
static int getRightMostSetBit( int n)
{
if (n == 0 )
return 0 ;
return ( int )((Math.log10(n & -n)) / Math.log10( 2 ))
+ 1 ;
}
static int posOfRightMostDiffBit( int m, int n)
{
return getRightMostSetBit(m ^ n);
}
public static void main(String arg[])
{
int m = 52 , n = 4 ;
System.out.print( "Position = "
+ posOfRightMostDiffBit(m, n));
}
}
|
Python3
import math
def getRightMostSetBit(n):
if (n = = 0 ):
return 0
return math.log2(n & - n) + 1
def posOfRightMostDiffBit(m, n):
return getRightMostSetBit(m ^ n)
if __name__ = = "__main__" :
m = 52
n = 4
print ( "position = " , int (posOfRightMostDiffBit(m, n)))
|
C#
using System;
class GFG {
static int getRightMostSetBit( int n)
{
if (n == 0)
return 0;
return ( int )((Math.Log10(n & -n)) / Math.Log10(2))
+ 1;
}
static int posOfRightMostDiffBit( int m, int n)
{
return getRightMostSetBit(m ^ n);
}
public static void Main()
{
int m = 52, n = 4;
Console.Write( "Position = "
+ posOfRightMostDiffBit(m, n));
}
}
|
PHP
<?php
function getRightMostSetBit( $n )
{
if ( $n == 0)
return 0;
return log( $n & - $n , (2)) + 1;
}
function posOfRightMostDiffBit( $m , $n )
{
return getRightMostSetBit( $m ^ $n );
}
$m = 52;
$n = 4;
echo posOfRightMostDiffBit( $m , $n );
?>
|
Javascript
<script>
function getRightMostSetBit(n)
{
if (n == 0)
return 0;
return Math.log2(n & -n) + 1;
}
function posOfRightMostDiffBit(m, n)
{
return getRightMostSetBit(m ^ n);
}
let m = 52, n = 24;
document.write( "Position of rightmost different bit:"
+ posOfRightMostDiffBit(m, n) + "<br>" );
</script>
|
Output
Position of rightmost different bit:3
Time Complexity: O(log2 N), this time complexity is equal to O(1) as one has to check for at most 31 bits only
Auxiliary Space: O(1)
Position of rightmost different bit using ffs() function:
ffs() function searches the first set bit from the right side and then returns the index of that bit (1-based indexing). So we can use this function on the XOR of both the values to get the index of the rightmost different bit
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int posOfRightMostDiffBit( int m, int n)
{
return ffs(m ^ n);
}
int main()
{
int m = 52, n = 4;
cout << "Position = " << posOfRightMostDiffBit(m, n);
return 0;
}
|
Java
import java.util.*;
class GFG {
static int posOfRightMostDiffBit( int m, int n)
{
return ( int )Math.floor(
Math.log10(Math.pow(m ^ n, 2 )))
+ 2 ;
}
public static void main(String[] args)
{
int m = 52 , n = 4 ;
System.out.println( "Position = "
+ posOfRightMostDiffBit(m, n));
}
}
|
Python3
from math import floor, log10
def posOfRightMostDiffBit(m, n):
return floor(log10( pow (m ^ n, 2 ))) + 2
if __name__ = = '__main__' :
m, n = 52 , 4
print ( "Position = " ,
posOfRightMostDiffBit(m, n))
|
C#
using System;
class GFG {
static int posOfRightMostDiffBit( int m, int n)
{
return ( int )Math.Floor(
Math.Log10(Math.Pow(m ^ n, 2)))
+ 2;
}
public static void Main(String[] args)
{
int m = 52, n = 4;
Console.Write( "Position = "
+ posOfRightMostDiffBit(m, n));
}
}
|
PHP
<?php
function posOfRightMostDiffBit( $m , $n )
{
$t = floor (log( $m ^ $n , 2));
return $t ;
}
$m = 52;
$n = 4;
echo "Position = " ,
posOfRightMostDiffBit( $m , $n );
?>
|
Javascript
<script>
function posOfRightMostDiffBit(m, n)
{
return parseInt(Math.floor
(Math.log10(Math.pow(m ^ n, 2))), 10) + 2;
}
let m = 52, n = 4;
document.write(
"Position = " + posOfRightMostDiffBit(m, n)
);
</script>
|
Time Complexity: O(log2 N), this time complexity is equal to O(1) as one has to check for at most 31 bits only
Auxiliary Space: O(1)
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
12 Sep, 2022
Like Article
Save Article