JuniorBit Manipulation
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 is111and 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
Input
start = 10, goal = 7Output
3The binary representations of 10 and 7 are 1010 and 0111, and they differ in 3 bit positions, so 3 flips are required.
Example 2
Input
start = 3, goal = 4Output
3The 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