Skip to content
RED SAGSkills

Count Differing Bits

Two firmware flag registers are stored as non-negative integers a and b. Return how many bit positions hold different values in the two registers.

Example 1

Input: a = 1, b = 4
Output: 2
Explanation: 001 vs 100 differ in two positions.

Example 2

Input: a = 3, b = 1
Output: 1

Constraints

  • 0 <= a, b <= 2^31 - 1
Hint 1

XOR the two values: the 1-bits of the result are exactly the differing positions.

Hint 2

x & (x - 1) clears the lowest set bit.

Good practice for: Embedded Systems Engineer, Game Developer, Backend Developer, Blockchain Developer, Automation Test Engineer

Run checks your code on 2 sample tests. Submit runs all 7 tests, including hidden ones, and earns points. Use console.log to debug. Python's first run can take a few seconds while it starts.