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.

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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++

 // C++ implementation to find the position // of rightmost same bit #include    using namespace std;    // Function to find the position of // rightmost set bit in 'n' int getRightMostSetBit(unsigned int n) {     return log2(n & -n) + 1; }    // Function to find the position of // rightmost same bit in the // binary representations of 'm' and 'n' int posOfRightMostSameBit(unsigned int m,                           unsigned int n) {     // position of rightmost same bit     return getRightMostSetBit(~(m ^ n)); }    // Driver program to test above int main() {     int m = 16, n = 7;     cout << "Position = "          << posOfRightMostSameBit(m, n);     return 0; }

Java

 // Java implementation to find the position // of rightmost same bit class GFG {                // Function to find the position of     // rightmost set bit in 'n'     static int getRightMostSetBit(int n)     {         return (int)((Math.log(n & -n))/(Math.log(2)))                                                   + 1;     }            // Function to find the position of     // rightmost same bit in the     // binary representations of 'm' and 'n'     static int posOfRightMostSameBit(int m,int n)     {                    // position of rightmost same bit         return getRightMostSetBit(~(m ^ n));     }            //Driver code     public static void main (String[] args)     {         int m = 16, n = 7;                    System.out.print("Position = "             + posOfRightMostSameBit(m, n));     } }    // This code is contributed by Anant Agarwal.

Python3

 # Python3 implementation to find the  # position of rightmost same bit import math    # Function to find the position  # of rightmost set bit in 'n' def getRightMostSetBit(n):        return int(math.log2(n & -n)) + 1    # Function to find the position of # rightmost same bit in the binary # representations of 'm' and 'n' def posOfRightMostSameBit(m, n):        # position of rightmost same bit     return getRightMostSetBit(~(m ^ n))    # Driver Code m, n = 16, 7 print("Position = ", posOfRightMostSameBit(m, n))    # This code is contributed by Anant Agarwal.

C#

 // C# implementation to find the position // of rightmost same bit using System;    class GFG {     // Function to find the position of     // rightmost set bit in 'n'     static int getRightMostSetBit(int n)     {         return (int)((Math.Log(n & -n)) / (Math.Log(2))) + 1;     }             // Function to find the position of     // rightmost same bit in the     // binary representations of 'm' and 'n'     static int posOfRightMostSameBit(int m,int n)     {         // position of rightmost same bit         return getRightMostSetBit(~(m ^ n));     }            //Driver code     public static void Main ()     {         int m = 16, n = 7;         Console.Write("Position = "               + posOfRightMostSameBit(m, n));     } } //This code is contributed by Anant Agarwal.

PHP



Output:

Position = 4

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.

My Personal Notes arrow_drop_up

Improved By : vt_m

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.