# 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
```

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.

## Java

 `// Java implementation to find the position  ` `// of rightmost same bit  ` `class` `GFG {  ` `     `  `    ``// Function to find the position of  ` `    ``// rightmost same bit in the  ` `    ``// binary representations of 'm' and 'n'  ` `    ``static` `int` `posOfRightMostSameBit(``int` `m,``int` `n)  ` `    ``{  ` `        ``int` `loopCounter = ``1``; ``// Initialize loop counter ` `        ``while` `(m > ``0` `|| n > ``0``){ ` `             `  `            ``boolean` `a = m%``2` `== ``1``; ``//Check whether the value 'm' is odd ` `            ``boolean` `b = n%``2` `== ``1``; ``//Check whether the value 'n' is odd ` `             `  `            ``// Below 'if' checks for both values to be odd or even ` `            ``if` `(!(a ^ b)){  ` `                ``return` `loopCounter; ` `             `  `            ``m = m >> ``1``; ``//Right shift value of m ` `            ``n = n >> ``1``; ``//Right shift value of n ` `            ``loopCounter++; ` `        ``} ` `        ``return` `-``1``; ``//When no common set is found  ` `    ``}  ` `       `  `    ``//Driver code  ` `    ``public` `static` `void` `main (String[] args)  ` `    ``{  ` `        ``int` `m = ``16``, n = ``7``;  ` `           `  `        ``System.out.print(``"Position = "` `            ``+ posOfRightMostSameBit(m, n));  ` `    ``}  ` `}  `

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.

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.

My Personal Notes arrow_drop_up

Improved By : vt_m, AyushKrishnaMurthi

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.