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
(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
(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
Approach: 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.
Explanation: The bitwise xor operation produces a number which has set bits only at the positions where the bits of m and n differ. Thus, the position of the rightmost set bit in xor_value gives the position of the rightmost different bit.
Below is the implementation of the above approach:
C++
// C++ implementation to find the position// of rightmost different bit#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 diffrent bit.int posOfRightMostDiffBit(int m, int n){ // position of rightmost different // bit return getRightMostSetBit(m ^ n);}// Driver programint main(){ int m = 52, n = 24; cout << "Position of rightmost diffrent bit:" << posOfRightMostDiffBit(m, n)<<endl; return 0;} |
Java
// Java implementation to find the position// of rightmost different bitclass 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); } // Driver code public static void main(String arg[]) { int m = 52, n = 4; System.out.print("Position = " + posOfRightMostDiffBit(m, n)); }}// This code is contributed by Anant Agarwal. |
Python3
# Python implementation# to find the position# of rightmost different bitimport 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)# Driver codem = 52n = 4print("position = ", int(posOfRightMostDiffBit(m, n)))# This code is contributed# by Anant Agarwal. |
C#
// C#implementation to find the position// of rightmost different bitusing 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; Console.Write("Position = " + posOfRightMostDiffBit(m, n)); }}// This code is contributed by Smitha. |
PHP
<?php// PHP implementation to // find the position of// rightmost different bit// 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);} // Driver Code $m = 52; $n = 4; echo posOfRightMostDiffBit($m, $n); // This code is contributed by Ajit?> |
Position of rightmost diffrent bit:3
Using ffs() function
C++
// C++ implementation to find the// position of rightmost different// bit in two number.#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);} // Driver codeint main(){ int m = 52, n = 4; cout <<"Position = " << posOfRightMostDiffBit(m, n); return 0; } |
Java
// Java implementation to find the// position of rightmost different// bit in two number.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;} // Driver codepublic static void main(String[] args){ int m = 52, n = 4; System.out.println("Position = " + posOfRightMostDiffBit(m, n));} } // This code is contributed by gauravrajput1 |
PHP
<?php// PHP implementation to find the// position of rightmost different// bit in two number.// 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 "Position = " , posOfRightMostDiffBit($m, $n);// This code is contributed by ajit?> |
Position = 5
This article is contributed by Ayush Jauhari. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Position of rightmost set bit
- Get the position of rightmost unset bit
- Position of rightmost common bit in two numbers
- Position of rightmost bit with first carry in sum of two binary
- Set the rightmost unset bit
- Set the rightmost unset bit
- Set the rightmost off bit
- Number formed by the rightmost set bit in N
- Turn off the rightmost set bit | Set 2
- Number formed by flipping all bits to the left of rightmost set bit
- Turn off the rightmost set bit
- Find position of the only set bit
- Modify a bit at a given position
- Check whether the two numbers differ at one bit position only
- Find position of left most dis-similar bit for two numbers
- Position of the K-th set bit in a number
- Find letter's position in Alphabet using Bit operation
- Check whether the bit at given position is set or unset
- Inserting m into n such that m starts at bit j and ends at bit i.
- Inserting M into N such that m starts at bit j and ends at bit i | Set-2

