Home | Projects | Notes > Problem Solving > LC - E - 2220. Minimum Bit Flips to Convert Number
This solution uses bitwise operations.
Complexity Analysis:
start ^ goal
Solution:
xxxxxxxxxx151int minBitFlips(int start, int goal){2 int count = 0;3
4 // 1. XOR two numbers -> result will contain only the different bits set5 start ^= goal;6
7 // 2. clear least set bit one by one and count them.8 while (start)9 {10 start &= (start - 1); // clear the lowest set bit of 'start'11 count++;12 }13
14 return count;15}