Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //{ Driver Code Starts
- //Initial Template for C++
- #include <bits/stdc++.h>
- using namespace std;
- // } Driver Code Ends
- //User function Template for C++
- class Solution
- {
- public:
- //Function to find the first position with different bits.
- int posOfRightMostDiffBit(int m, int n)
- {
- // Your code here
- int ans=-1;
- int i=1;
- if(m==0 || n==0)
- {
- if(n==0)
- {
- while(m!=0)
- {
- if((m&1)!=(n&1))
- {
- ans=i;
- break;
- }
- m=m>>1;
- i++;
- }
- }
- else if(m==0)
- {
- while(n!=0)
- {
- if((m&1)!=(n&1))
- {
- ans=i;
- break;
- }
- n=n>>1;
- i++;
- }
- }
- }
- else
- {
- while(m!=0 || n!=0)
- {
- if((m&1)!=(n&1))
- {
- ans=i;
- break;
- }
- m=m>>1;
- n=n>>1;
- i++;
- }
- }
- return ans;
- }
- };
- //{ Driver Code Starts.
- // Driver Code
- int main()
- {
- int t;
- cin>>t; //input number of testcases
- while(t--)
- {
- int m,n;
- cin>>m>>n; //input m and n
- Solution ob;
- cout << ob.posOfRightMostDiffBit(m, n)<<endl;
- }
- return 0;
- }
- // } Driver Code Ends
Advertisement
Add Comment
Please, Sign In to add comment