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 differentInput: 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
Table of Content
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:
For n = 10, which is 00001010 in binary, we can find the position of its rightmost set bit using the expression log2(n & -n) + 1. First, we calculate -n using two's complement by inverting the bits of 10 to get 11110101 and adding 1 to obtain 11110110. Performing (n & -n) gives 00000010, which is 2 in decimal. Taking log2(2) results in 1, and adding 1 gives us 2, indicating that the rightmost set bit of 10 is at position 2 from the right. This method efficiently isolates the rightmost set bit and calculates its position using bitwise operations and logarithms.
#include <bits/stdc++.h>
using namespace std;
// Function to find the position of
// rightmost set bit in 'n'
// returns 0 if there is no set bit.
int getRightMostSetBit(int n)
{
// to handle edge case when n = 0.
if (n == 0)
return 0;
return log2(n & -n) + 1;
}
// Function to find the position of
// rightmost different bit in the
// binary representations of 'm' and 'n'
// returns 0 if there is no
// rightmost different bit.
int posOfRightMostDiffBit(int m, int n)
{
// position of rightmost different
// bit
return getRightMostSetBit(m ^ n);
}
int main()
{
int m = 52, n = 4;
cout << posOfRightMostDiffBit(m, n) << endl;
return 0;
}
class GFG {
// Function to find the position of
// rightmost set bit in 'n'
// return 0 if there is no set bit.
static int getRightMostSetBit(int n)
{
if (n == 0)
return 0;
return (int)((Math.log10(n & -n)) / Math.log10(2))
+ 1;
}
// Function to find the position of
// rightmost different bit in the
// binary representations of 'm' and 'n'
static int posOfRightMostDiffBit(int m, int n)
{
// position of rightmost different bit
return getRightMostSetBit(m ^ n);
}
public static void main(String arg[])
{
int m = 52, n = 4;
System.out.print( + posOfRightMostDiffBit(m, n));
}
}
import math
# Function to find the position of
# rightmost set bit in 'n'
def getRightMostSetBit(n):
if (n == 0):
return 0
return math.log2(n & -n) + 1
# Function to find the position of
# rightmost different bit in the
# binary representations of 'm' and 'n'
def posOfRightMostDiffBit(m, n):
# position of rightmost different
# bit
return getRightMostSetBit(m ^ n)
if __name__ == "__main__":
m = 52
n = 4
print(int(posOfRightMostDiffBit(m, n)))
using System;
class GFG {
// Function to find the position of
// rightmost set bit in 'n'
static int getRightMostSetBit(int n)
{
if (n == 0)
return 0;
return (int)((Math.Log10(n & -n)) / Math.Log10(2))
+ 1;
}
// Function to find the position of
// rightmost different bit in the
// binary representations of 'm' and 'n'
static int posOfRightMostDiffBit(int m, int n)
{
// position of rightmost different bit
return getRightMostSetBit(m ^ n);
}
// Driver code
public static void Main()
{
int m = 52, n = 4;
// Function call
Console.Write( posOfRightMostDiffBit(m, n));
}
}
// Function to find the position
// of rightmost set bit in 'n'
function getRightMostSetBit(n) {
if (n === 0)
return 0;
return Math.log2(n & -n) + 1;
}
// Function to find the position
// of rightmost different bit
function posOfRightMostDiffBit(m, n) {
return getRightMostSetBit(m ^ n);
}
// Driver code
let m = 52, n = 4;
console.log(posOfRightMostDiffBit(m, n));
<?php
// Function to find the position
// of rightmost set bit in 'n'
function getRightMostSetBit($n)
{
if ($n == 0)
return 0;
return log($n & -$n, (2)) + 1;
}
// Function to find the position of
// rightmost different bit in the
// binary representations of 'm'
// and 'n'
function posOfRightMostDiffBit($m, $n)
{
// position of rightmost
// different bit
return getRightMostSetBit($m ^ $n);
}
$m = 52;
$n = 4;
echo posOfRightMostDiffBit($m, $n);
?>
Output
5
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
#include <bits/stdc++.h>
using namespace std;
// function to find rightmost different
// bit in two numbers.
int posOfRightMostDiffBit(int m, int n)
{
return ffs(m ^ n);
}
int main()
{
int m = 52, n = 4;
// Function call
cout << posOfRightMostDiffBit(m, n);
return 0;
}
import java.util.*;
class GFG {
// function to find rightmost
// different bit in two numbers.
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;
// Function call
System.out.println(posOfRightMostDiffBit(m, n));
}
}
from math import floor, log10
# Function to find rightmost different
# bit in two numbers.
def posOfRightMostDiffBit(m, n):
return floor(log10(pow(m ^ n, 2))) + 2
if __name__ == '__main__':
m, n = 52, 4
print(
posOfRightMostDiffBit(m, n))
using System;
class GFG {
// function to find rightmost
// different bit in two numbers.
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( posOfRightMostDiffBit(m, n));
}
}
// This code is contributed by shivanisinghss2110
// Function to find the position of rightmost different bit
function posOfRightMostDiffBit(m, n) {
let x = m ^ n;
if (x === 0)
return 0;
return Math.log2(x & -x) + 1;
}
// Driver code
let m = 52, n = 4;
console.log(posOfRightMostDiffBit(m, n));
<?php
// function to find rightmost
// different bit in two numbers.
function posOfRightMostDiffBit($m, $n)
{
$t = floor(log($m ^ $n, 2));
return $t;
}
// Driver code
$m = 52;
$n = 4;
echo posOfRightMostDiffBit($m, $n);
?>
Output
5
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)