Find the minimum number of single-bit flips needed to change one integer into another.
Problem
Given two integers start and goal, determine the minimum number of bit positions that must be flipped in start so that it becomes equal to goal.
A bit flip changes a 0 to 1 or a 1 to 0 at exactly one position.
Intuition
Only positions where the two numbers differ need to be flipped, so the answer is the number of differing bits in their binary representations.
Input Format
- Two integers
startandgoal. - You may assume standard 32-bit signed integers for interview-style reasoning unless otherwise specified.
Output Format
- Return a single integer: the minimum number of bit flips required to transform
startintogoal.
Constraints
0 <= start, goal <= $10^{9}$is a reasonable interview-style assumption.- The answer is the Hamming distance between the two bit patterns.
- Use only as much bit width as needed to compare the values.
Example 1
Input
start = 10, goal = 7
Output
3
Explanation
10 = 1010 and 7 = 0111. The bits differ in three positions, so 3 flips are needed.
Example 2
Input
start = 3, goal = 4
Output
3
Explanation
3 = 0011 and 4 = 0100. All three relevant bit positions differ, so the minimum number of flips is 3.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.