Skip to main content

2220. Minimum Bit Flips to Convert Number

class Solution {
public:
int minBitFlips(int start, int goal)
{
int n = start ^ goal;
int cnt = 0;
while (n)
{
n &= (n - 1);
cnt++;
}
return cnt;
}
};
  • T: O(numberofbits)O(number of bits)
  • S: O(1)O(1)