Minimum Bit Flips to Convert Number (LeetCode 2220): Solution and Explanation
Return how many single-bit flips turn start into goal, which is the number of bit positions where they differ.
Intuition
A flip touches one bit and nothing else, so bits never interact. A position where start and goal already agree needs no flip, and a position where they disagree needs exactly one.
XOR is the tool that marks disagreement: a bit of start ^ goal is 1 exactly where the two numbers differ. The answer is the number of 1s in that value.
To count the 1s, use diff & (diff - 1). Subtracting 1 turns the lowest 1 into 0 and the zeros below it into 1s, so the AND wipes out precisely that lowest 1. The loop runs once per differing bit, no more.
Approach: XOR plus Kernighan bit count
- Compute
diff = start ^ goal. - Set
flips = 0. - While
diffis not 0, setdiff &= diff - 1and add one toflips. - Return
flips.
Step-by-step example
start = 10, goal = 7
- 10 is 1010 and 7 is 0111, so diff = 1101 (13).
- 1101 & 1100 = 1100: bit 0 cleared, flips = 1.
- 1100 & 1011 = 1000: bit 2 cleared, flips = 2.
- 1000 & 0111 = 0000: bit 3 cleared, flips = 3.
- diff is 0, so return 3.
Code: Python, Java, C++
1class Solution:
2 def minBitFlips(self, start: int, goal: int) -> int:
3 diff = start ^ goal
4 flips = 0
5 while diff:
6 diff &= diff - 1
7 flips += 1
8 return flips1class Solution {
2 public int minBitFlips(int start, int goal) {
3 int diff = start ^ goal;
4 int flips = 0;
5 while (diff != 0) {
6 diff &= diff - 1;
7 flips++;
8 }
9 return flips;
10 }
11}1class Solution {
2public:
3 int minBitFlips(int start, int goal) {
4 int diff = start ^ goal;
5 int flips = 0;
6 while (diff != 0) {
7 diff &= diff - 1;
8 flips++;
9 }
10 return flips;
11 }
12};Time and space complexity
O(k)
The loop runs once per differing bit, k, which is at most 30 for values up to 10^9.
O(1)
Only diff and the counter are kept.
Other approaches
Built-in popcount (time O(1), space O(1))
Return the built-in bit count of start ^ goal: bin(x).count('1') in Python, Integer.bitCount in Java, __builtin_popcount in C++, bits.OnesCount in Go.
Compare bit by bit (time O(log max(start, goal)), space O(1))
Loop while either number is non-zero, compare the low bits with (start & 1) != (goal & 1), then shift both right. Visits every bit up to the longer length, not just the differing ones.
Common mistakes
- Counting bits of start and goal separately and subtracting. Two numbers with the same number of 1s can still differ in many positions.
- Shifting a negative number right in languages with arithmetic shift. It never happens here since inputs are non-negative, but the XOR-and-clear loop avoids shifts entirely.
- Using diff & (diff + 1) or diff | (diff - 1). Only diff & (diff - 1) clears the lowest set bit.
Frequently asked questions
Why does diff & (diff - 1) remove exactly one 1?
Subtracting 1 flips the lowest 1 to 0 and every 0 below it to 1, leaving higher bits unchanged. ANDing with the original keeps the higher bits and zeroes the lowest 1 and everything under it.
Is this the same as Hamming Distance?
Yes. LeetCode 461 asks for the number of differing bit positions between two integers, which is the same quantity under a different story.
Why can leading zeros be flipped without changing the answer?
Flipping a leading zero of start only helps if goal has a 1 there, and that position already shows up as a 1 in start ^ goal. Positions above both numbers' highest 1 are 0 in both, so they never need a flip.