Count Bits to Convert A to B
Implement countBitsToConvert
Given two non-negative integers
a and b, count how many bit positions need to be flipped to turn a into b.
A bit position needs flipping exactly where a and b disagree — and XOR is built to flag exactly that: a ^ b has a 1 in every position where the two numbers differ, and a 0 everywhere they agree. Counting the set bits in that XOR result, the same set-bit-counting trick used earlier, gives the answer directly.
Example 1:
Input: a = 10, b = 20
Output: 4
Example 2:
Input: a = 0, b = 0
Output: 0
Example 3:
Input: a = 7, b = 8
Output: 4
+ 8 hidden test cases run on Submit.
Constraints:
- ●
0 ≤ a, b ≤ 2³¹ − 1
a =
10
b =
20