← All Patterns

Pattern Recognition Drill

#87

Easy Bit Manipulation

The Problem

Count positions where corresponding bits of two integers differ.

What approach would you use?

Think about it before scrolling down.

Hamming distance

Key Signals

Bit Manipulation

XOR a and b, count set bits in the result. Combines XOR + Kernighan's trick. O(1).

Common Trap

This is a two-step pattern: XOR to find differences, then count bits.

← #86 #88 →