Minimum Bit Flips to Convert Number

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.

  • For example, for x = 7, the binary representation is 111 and we may choose any bit, including leading zeros not shown, and flip it.

Given two integers start and goal, return the minimum number of bit flips to convert start to goal.

Note: This question is the same as 461: Hamming Distance.

Example 1
Inputstart = 10, goal = 7
Output3
The binary representations of 10 and 7 are 1010 and 0111, and they differ in 3 bit positions, so 3 flips are required.
Example 2
Inputstart = 3, goal = 4
Output3
The binary representations of 3 and 4 are 011 and 100, and they differ in 3 bit positions, so 3 flips are required.

Constraints

  • 0 <= start, goal <= 10^9

Asked at 7 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate