-
Notifications
You must be signed in to change notification settings - Fork 0
/
no of bits to be flipped
57 lines (38 loc) · 1 KB
/
no of bits to be flipped
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
57
//{ Driver Code Starts
//Initial Template for Java
import java.util.*;
import java.lang.*;
import java.io.*;
// } Driver Code Ends
//User function Template for Java
class Solution{
// Function to find number of bits needed to be flipped to convert A to B
public static int countBitsFlip(int a, int b){
// Your code here
int x = a^b;
int ans = 0;
while(x!=0){
x=x&(x-1);
ans++;
}
return ans;
}
}
//{ Driver Code Starts.
// Driver class
class Main {
public static void main(String[] args) throws NumberFormatException, IOException {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();//testcases
int m, n;
long ans = 0; // initialise ans to 0
while(t-->0) {
//input m and n
m = sc.nextInt();
n = sc.nextInt();
Solution obj = new Solution();
System.out.println(obj.countBitsFlip(m, n));
}
}
}
// } Driver Code Ends