Rightmost different bit | Practice | GeeksforGeeks

Given two numbers M and N. The task is to find the position of the rightmost different bit in the binary representation of numbers.
Example 1: 
Input: M = 11, N = 9
Output: 2
Explanation: Binary representation of the given 
numbers are: 1011 and 1001, 
2nd bit from right is different.
Example 2:
Input: M = 52, N = 4
Output: 5
Explanation: Binary representation of the given 
numbers are: 110100 and 0100, 
5th-bit from right is different.

User Task:
The task is to complete the function posOfRightMostDiffBit() which takes two arguments m and n and returns the position of first different bits in m and n. If both m and n are the same then return -1 in this case.
Expected Time Complexity: O(max(log m, log n)).
Expected Auxiliary Space: O(1).
Constraints:
0 <= M <= 109
0 <= N <= 109
0
vsk63704 days ago
easy to understand:
int posOfRightMostDiffBit(int m, int n)   {       if(m==n){           return -1;       }
       int result=m^n;              int count=0;       while((result&1) !=1){           count+=1;           result=result>>1;       }       return (count+1);   }
0
mrnecto1 week ago
class Solution
{
   public:
   //Function to find the first position with different bits.
   int posOfRightMostDiffBit(int m, int n)
   {
       // Your code here
       m = m^n;
       int cnt = 1;
       while(m > 0)
       {
           if(m & 0x01) return cnt;
           cnt++;
           m >>= 1;
       }
       return -1;
   }
};
0
sachinupreti1901 week ago
// Here we go.
 
class Solution{   public:   //Function to find the first position with different bits.   int posOfRightMostDiffBit(int m, int n)   {       // Your code here       string p,q;       int i;       bitset<32>a(m);       bitset<32>b(n);       if(a==b)       return -1;       else       {           p=a.to_string();           q=b.to_string();           reverse(p.begin(),p.end());           reverse(q.begin(),q.end());           for(i=0;i<p.size();i++)           {               if(p[i]==q[i])               continue;               else               break;           }           return i+1;       }   }};
+1
atulharsh274Premium1 week ago
// one line solution for this problem
return m==n?-1: log2( (-(m^n)) & (m^n)) + 1;
0
harshpandeyalfa22 weeks ago
C++
(0.01sec)
int posOfRightMostDiffBit(int m, int n)   {       int count=1;       while( m || n)       {            if( (1&m) != (1&n))             return count;           else if( (1&m) == (1&n))            count++;            if(m)              m=m>>1;              if(n)               n=n>>1;       }       return -1;   }
+1
anuraggulati2412 weeks ago
simple LOG(N^M) SOLUTION...
 
int posOfRightMostDiffBit(int m, int n)
    {
       
       if(m != n) {
        // Your code here
        // first find xor of both m and n...
       int Xor = m ^ n;
       // now we see the first det bit in xor ...
       int value = Xor & ~(Xor - 1);
       // now finded the position of the set bit...
       int position = log2(value) + 1;
       // return that position...
       return position;
       }
       
       else {
           return -1;
       }
        
    }
0
asifsaba5812 weeks ago
class Solution
{
    public:
    //Function to find the first position with different bits.
    int posOfRightMostDiffBit(int m, int n)
    {
        //for example 11 and 9;
        
        int count=0;
        
        int zor=m^n;  // 1011^1001=0010; // 2
        
        if(zor==0){return -1;} // false;
        
        while(zor>0){   //2>0   // 1>0
            
            if((zor&1)==0)  0010&0001=0000 // true 0001&0001=0001 false
            {
                
                count++;   //1
                
                zor=zor>>1;   0010>>1=0001 //again it will go for while loop.
            }
            else{
                
                count++;  //1
                
                break;
            }
        }
        return count; //1+1=2
    }
};
0
anirbandam103 weeks ago
int posOfRightMostDiffBit(int m, int n)   {       if (m == n) return -1;       return log2((m^n) & ~((m^n)-1)) + 1;   }
0
gauty verma3 weeks ago
private static int posOfRightMostDiffBit(int m, int n) { // TODO Auto-generated method stub int res = 1; if(m==n) {  return -1; } int xor = m^n; int val = 1; while(true) {  if((xor & val) != 0) {   return res;  }  val = val<<1;  res++; }}
0
rahuldebnath294 weeks ago
int posOfRightMostDiffBit(int m, int n)   {       // Your code here       int x=m^n;       for ( int i = 0; i < 32; i++){                      if(x&(1<<i))           return i+1;       }       return -1;   }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.