Description
A bit flip of a number x is choosing a bit in the binary representation of x and flipping it from either 0 to 1 or 1 to 0. Return the minimum number of bit flips to convert start to goal.
Examples
Input:
start = 10, goal = 7Output:
3Explanation:
1010 -> 0111 needs 3 flips.
Input:
start = 0, goal = 0Output:
0Explanation:
Both numbers are already equal, so no bit flips are needed.
Input:
start = 15, goal = 8Output:
3Explanation:
Binary: 1111 -> 1000. Need to flip bits at positions 0, 1, and 2 (rightmost bits), so 3 flips total.
Constraints
- •
0 ≤ start, goal ≤ 10⁹