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.
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 <bits/stdc++.h> 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, 7print("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
<?php // PHP implementation to // find the position // of rightmost same bit // Function to find the position of // rightmost set bit in 'n' function getRightMostSetBit($n) { return log($n & -$n) + 1; } // Function to find the position of // rightmost same bit in the // binary representations of 'm' and 'n' function posOfRightMostSameBit($m, $n) { // position of rightmost same bit return getRightMostSetBit(~($m ^ $n)); } // Driver Code $m = 16; $n = 7; echo "Position = " , ceil(posOfRightMostSameBit($m, $n)); // This code is contributed by anuj_67. ?> |
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.
Recommended Posts:
- Position of rightmost different bit
- Position of rightmost set bit
- Get the position of rightmost unset bit
- Position of rightmost bit with first carry in sum of two binary
- Check whether the two numbers differ at one bit position only
- Find position of left most dis-similar bit for two numbers
- Set the rightmost off bit
- Set the rightmost unset bit
- Set the rightmost unset bit
- Turn off the rightmost set bit
- Number formed by the rightmost set bit in N
- Find position of the only set bit
- Modify a bit at a given position
- Position of the K-th set bit in a number
- Check whether the bit at given position is set or unset
Improved By : vt_m



