Minimum Bit Flips

Easy
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start to step? all shortcuts

Solution Guide: Minimum Bit Flips

Approach

XOR start and goal to highlight the bits that differ. Count the set bits in the XOR result using Brian Kernighan's algorithm. Each set bit represents one required flip.

Complexity Analysis

Time
O(1)
Space
O(1)

Pattern

XOR Pattern

Why It Works

XOR marks exactly the positions where the two numbers differ, and the count of those 1-bits equals the minimum number of single-bit flips needed to convert one number to the other.

Updated Feb 2026