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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right



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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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)); 
    

chevron_right



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.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




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.