


Ninja is learning the binary representation of the numbers. He wanted to practice the topic, so he picked a question. The problem statement says, two numbers, ‘A’ and ‘B’ are given. Find the number of bits of ‘B’ that should be flipped to convert it into ‘A’.Can you help Ninja to solve this problem?
You are given two integers, ‘A’ and ‘B’.Find the number of bits of ‘B’ that should be flipped to convert it into ‘A’.
For ExampleIf ‘A’ is 13(1101) and ‘B’ is 7(0111), The number of bits that should be flipped is 2(0111).
The first line of the input contains an integer, 'T,’ denoting the number of test cases.
The first line of each test case contains two integers, ‘A’ and ‘B’.
Output Format:
For each test case, print ‘an integer corresponding to the minimum number of swaps required.
Print the output of each test case in a separate line.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= ‘A’,’B’ <= 10^9.
Time limit: 1 sec
2
13 7
15 20
2
4
For the first test case,
The binary representation of 13 is 1101.
The binary representation of 7 is 0111.
So, we will change the 2nd and the 4th bit from the right to convert B into A.
Hence, the answer is 2.
For the second test case:
The binary representation of 20 is 10100.
The binary representation of 15 is 01111.
So, we will change the 1st,2nd,4th, and 5th bit from the right to convert B into A.
Hence, the answer is 4.
2
3 10
6 4
2
1
Check every bit of ‘A’ and ‘B’.
In this approach, we will simply iterate through all the bits of ‘A’ and ‘B’ and count the number of bits that are not matching, as if we just change these mismatched bits, we will find the number of bits that should be flipped.
At last, we will return ‘ANS’ storing the number of flips required.
Algorithm:
O(log(maximum of A and B)), where ‘A’ and ‘B’ are the given numbers.
In this approach, we traverse all the bits of ‘A’ and ‘B’.The number of bits of ‘A’ is of the order O(log(A)). Hence, the overall time complexity is O(log(maximum of A and B)).
O(1).
In this approach, we are using constant space. Hence, the overall space complexity is O(1).