-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bit Difference.cpp
56 lines (48 loc) · 1.02 KB
/
Bit Difference.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//{ 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 number of bits needed to be flipped to convert A to B
int countBitsFlip(int a, int b){
int count=0;
while(a>0 && b>0){
if(a%2==1 && b%2==0) count++;
else if(a%2==0 && b%2==1 ) count++;
a=a/2;
b=b/2;
}
if(a>0){
while(a>0){
if(a%2==1) count++;
a=a/2;
}
}
if(b>0){
while(b>0){
if(b%2==1) count++;
b=b/2;
}
}
return count;
}
};
//{ Driver Code Starts.
// Driver Code
int main()
{
int t;
cin>>t;// input the testcases
while(t--) //while testcases exist
{
int a,b;
cin>>a>>b; //input a and b
Solution ob;
cout<<ob.countBitsFlip(a, b)<<endl;
}
return 0;
}
// } Driver Code Ends