Minimum Bit Flips to Convert Number
Minimum Bit Flips to Convert Number is LeetCode 2220 (Easy). A bit flip changes one bit of a number from 0 to 1 or from 1 to 0, at any position, leading zeros included. Return the fewest flips that turn start into goal.
- 0 <= start, goal <= 10⁹
Intuition
Flipping one bit never changes any other bit. So each position can be settled on its own: it needs exactly one flip if start and goal disagree there, and none if they agree. The answer is the number of positions where the two numbers differ, which is their Hamming distance.
XOR finds those positions in one step: a bit of start ^ goal is 1 exactly where the two bits differ. All that is left is counting the 1s in that number. The expression x & (x - 1) removes the lowest 1 of x, so applying it until x reaches 0 takes one round per differing position.
"How many positions differ between two numbers" is a count of the 1s in their XOR.
Approach
Before reading on, decide whether the order of the flips can ever change the answer. Then find one operator that marks every position where the two numbers disagree. Aim for a loop that runs once per differing bit.
XOR the two numbers
Compute diff = start ^ goal. Where the bits agree XOR gives 0, and where they differ it gives 1, so diff marks every position that needs a flip and no other position.
Clear the lowest 1
Set diff = diff & (diff - 1). Subtracting 1 turns the lowest 1 into 0 and every 0 below it into 1. The AND then keeps only the bits above that point, so exactly one 1 disappears.
Count until nothing is left
Add 1 to flips for every bit cleared and stop when diff is 0. Each round counts one differing position, so the loop runs as many times as there are flips to make, at most 30 for numbers up to 10⁹.
Code in Python, C++ and Java
Common pitfalls
Comparing binary strings of different lengths
a, b = bin(start)[2:], bin(goal)[2:] return sum(x != y for x, y in zip(a, b))
return bin(start ^ goal).count("1")zip stops at the shorter string and lines the bits up from the left. For start = 3 (11) and goal = 4 (100) it compares only two pairs and returns 1, but 011 and 100 differ in all 3 positions.
Stopping when one number runs out of bits
while start and goal:
flips += (start & 1) != (goal & 1)
start >>= 1
goal >>= 1while start or goal:
flips += (start & 1) != (goal & 1)
start >>= 1
goal >>= 1The high bits of the larger number still need flipping after the smaller one reaches 0. For start = 8, goal = 1, the and loop stops after one round and returns 1, but 1000 and 0001 differ in 2 positions.
Edge cases
XOR gives 0, the loop never runs, and the answer is 0.
XOR returns the other number unchanged, so the answer is simply how many 1s that number has.
Complexity
Minimum Bit Flips to Convert Number FAQ
Is this the same problem as Hamming Distance?
Yes. LeetCode 461 asks for the number of differing bit positions between x and y, which is exactly the number of flips here. The same code passes both.
Can I use a built-in bit count instead of the loop?
Yes: (start ^ goal).bit_count() in Python 3.10+, __builtin_popcount(start ^ goal) in C++ and Integer.bitCount(start ^ goal) in Java all return the answer directly. Interviewers often ask for the loop as a follow-up, so know both.