description
If you want to convert an integer n to m, how many bits need to be changed?
Description
Both n and m are 32-bit integers.
Sample
- Example 1:
Input: n = 31, m = 14
Output: 2
Explanation:
(11111) -> (01110) there are two different bits.
- Example 2:
Input: n = 1, m = 7
Output: 2
Explanation:
(001) -> (111) will change two bits.
challenge
How many ways can you think of?
Parsing
Various operations in binary are used here
bitSwapRequired = (a, b) => {
var s = 0, c;
for (c = a ^ b; c !== 0; c = c >>> 1) {
s += c & 1;
}
return s;
}