Ctrl K

Count the Differing Bits

Easy
You are given two non-negative integers a and b. Write both in binary, aligned at the lowest bit (missing high bits count as zeros). Return the number of positions where the bits of a and b differ.
Example 1
a=5, b=12 → 2

5 = 0101 and 12 = 1100: the bits differ in the highest and the lowest position, 2 in total.

Example 2
a=9, b=9 → 0

The numbers are equal, so every bit matches.

Example 3
a=0, b=255 → 8
Constraints
  • 0 ≤ a, b ≤ 2³¹ − 1
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSum of Binary Strings EasyThe Unpaired Value EasyFlip the Bit Order EasyCount the Set Bits EasySize of a Packed Tree Easy
loading Python…
Input
a=5, b=12
Expected
2

Run checks the first 3 tests in your browser. Submit checks all 86 tests on the server, hidden ones included.