LeetCode #2220 Easy

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.

Constraints
  • 0 <= start, goal <= 10⁹
bit-manipulationxor
Open on LeetCode ↗
02

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 to spot this pattern

"How many positions differ between two numbers" is a count of the 1s in their XOR.

03

Approach

Try it first

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.

1

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.

2

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.

3

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⁹.

04

Code in Python, C++ and Java

▶1class Solution:
▶2 def minBitFlips(self, start, goal):
▶3 diff = start ^ goal
▶4 flips = 0
▶5 while diff:
▶6 diff &= diff - 1
▶7 flips += 1
▶8 return flips
8421start101010goal01117differstart XOR goalleft to countminus 1ANDflipswrite both numbers in binary
start10 = 1010
goal7 = 111
Line the two numbers up bit by bit, padding the shorter one with leading zeros: a flip may change a leading zero too. Each column is one bit position, worth the value printed above it.
8421start101010goal01117differ110113left to countminus 1ANDflips3 positions differ → those need flips
diff13 = 1101start ^ goal
XOR puts a 1 where the two bits differ and a 0 where they match. The highlighted columns are the only positions that need a flip; flipping a bit never affects another column, so the answer is how many 1s this row has.
8421start101010goal01117differ110113left to countminus 1AND110113110012110012flips1clear the 1 worth 1 → 1 flip so far
diff13 → 12diff & (diff - 1)
flips1
Subtracting 1 from 13 turns its lowest 1 (worth 1) into 0; there are no bits below it. ANDing with the original keeps only the bits above that point, so exactly one 1 disappears, leaving 12. One differing position is counted.
8421start101010goal01117differ110113left to countminus 1AND11001210111110008flips12clear the 1 worth 4 → 2 flips so far
diff12 → 8diff & (diff - 1)
flips2
Subtracting 1 from 12 turns its lowest 1 (worth 4) into 0 and the zeros below it into 1s. ANDing with the original keeps only the bits above that point, so exactly one 1 disappears, leaving 8. One differing position is counted.
8421start101010goal01117differ110113left to countminus 1AND100080111700000flips123clear the 1 worth 8 → 3 flips so far
diff8 → 0diff & (diff - 1)
flips3
Subtracting 1 from 8 turns its lowest 1 (worth 8) into 0 and the zeros below it into 1s. ANDing with the original keeps only the bits above that point, so exactly one 1 disappears, leaving 0. One differing position is counted.
8421start101010goal01117differ110113left to countminus 1ANDnothing left to countflips123nothing left → answer 3
result3
The value left to count is 0, so the loop stops after 3 rounds, one for each position where start and goal differ. Flipping exactly those bits turns 10 into 7.
05

Common pitfalls

Comparing binary strings of different lengths

✗ Wrong
a, b = bin(start)[2:], bin(goal)[2:]
return sum(x != y for x, y in zip(a, b))
✓ Right
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

✗ Wrong
while start and goal:
    flips += (start & 1) != (goal & 1)
    start >>= 1
    goal >>= 1
✓ Right
while start or goal:
    flips += (start & 1) != (goal & 1)
    start >>= 1
    goal >>= 1

The 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.

06

Edge cases

start equals goal

XOR gives 0, the loop never runs, and the answer is 0.

One number is 0

XOR returns the other number unchanged, so the answer is simply how many 1s that number has.

07

Complexity

Time
O(k)
Space
O(1)
k is the number of positions where start and goal differ. Each round clears one of them, and numbers up to 10⁹ have at most 30 bits, so k ≤ 30.
08

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.