- describe
-
Given two positive decimal integers A and B, figure out how many bits A and B differ in binary representation.
For example, the binary representation of "3" is "11", and the binary representation of "9" is "1001". "11" is less than 4 bits, and the insufficient part is actually 0, that is, "0011", so the two are from right to left. The 2nd and 4th bits are different, so 3 and 9 are two different when represented in binary. - enter
- The input consists of two lines, the first line is an integer n, indicating that there are n sets of test data. Immediately following n lines, each line includes two decimal positive integers A and B, separated by spaces.
- output
- The output has n lines, which are the number of bits that are different when A and B are represented in binary for each set of test data.
- sample input
-
1 3 9
- Sample output
-
2
Problem analysis: (omitted)
Program description: The XOR operation can obtain the difference of the bits.
#include <iostream> using namespace std; intmain() { int n, a, b, c, count; cin>>n; while(n--) { cin>>a>>b; c = a ^ b; count = 0; while(c) { if(c & 1) count++; c >>= 1; } cout<<count; } return 0; }