# Position of rightmost different bit

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

## Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

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 rightmost set bit in xor_value gives the position of rightmost different bit.

## C++

 `// C++ implementation to find the position ` `// of rightmost different bit ` `#include ` `using` `namespace` `std; ` `  `  `// Function to find the position of  ` `// rightmost set bit in 'n' ` `int` `getRightMostSetBit(``int` `n) ` `{ ` `    ``return` `log2(n & -n) + 1; ` `} ` `  `  `// Function to find the position of  ` `// rightmost different bit in the  ` `// binary representations of 'm' and 'n' ` `int` `posOfRightMostDiffBit(``int` `m, ``int` `n) ` `{ ` `    ``// position of rightmost different ` `    ``//  bit ` `    ``return` `getRightMostSetBit(m ^ n); ` `} ` `  `  `// Driver program ` `int` `main() ` `{ ` `    ``int` `m = 52, n = 4; ` `    ``cout << posOfRightMostDiffBit(m, n); ` `    ``return` `0;      ` `}  `

## Java

 `// Java implementation to find the position ` `// of rightmost different bit ` ` `  `class` `GFG { ` `     `  `    ``// Function to find the position of ` `    ``// rightmost set bit in 'n' ` `    ``static` `int` `getRightMostSetBit(``int` `n)  ` `    ``{ ` `        ``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 bit ` ` `  `import` `math ` ` `  `# Function to find the position of  ` `# rightmost set bit in 'n' ` `def` `getRightMostSetBit(n): ` ` `  `    ``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 code ` ` `  `m ``=` `52` `n ``=` `4` `print``(``"position = "``, ``int``(posOfRightMostDiffBit(m, n))) ` ` `  `# This code is contributed ` `# by Anant Agarwal. `

## C#

 `// C#implementation to find the position ` `// of rightmost different bit ` `using` `System; ` ` `  `class` `GFG { ` `     `  `    ``// Function to find the position of ` `    ``// rightmost set bit in 'n' ` `    ``static` `int` `getRightMostSetBit(``int` `n)  ` `    ``{ ` `        ``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

 ` `

Output:

```5
```

Using ffs() function

## C++

 `// C++ implementation to find the ` `// position of rightmost different ` `// bit in two number. ` `#include ` `using` `namespace` `std; ` `  `  `// function to find rightmost different ` `//  bit in two numbers. ` `int` `posOfRightMostDiffBit(``int` `m, ``int` `n) ` `{ ` `    ``return` `ffs(m ^ n); ` `} ` `  `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `m = 52, n = 4; ` `    ``cout <<``"Position = "` `<<  ` `         ``posOfRightMostDiffBit(m, n); ` `    ``return` `0;  ` `}  `

## PHP

 ` `

Output :

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

My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :

1

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