Simple C++ Solution || Runtime that beats 100% || BIT Manipulation || Time and Space Complexity ||
Intuition\n Describe your first thoughts on how to solve this problem. \n\nThe given problem is to find the minimum number of bit flips required to make the bit
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n![image.png](https://assets.leetcode.com/users/images/1d5864b0-8e6c-49d2-a028-777bb7ec7b6b_1686147324.2414613.png)\nThe given problem is to find the minimum number of bit flips required to make the bitwise OR of a and b equal to c. We can iterate over each bit position from the least significant bit to the most significant bit and check the conditions to determine if a bit flip is required at that position.\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1.Initialize a variable flips to keep track of the number of flips required.\n2.Iterate from i = 0 to i <= max(max(a, b), c).\n3.At each iteration:\n(a)Create a bit mask bit with the i-th bit set to 1 (1 << i).\n(b)Extract the i-th bit from a, b, and c using bitwise AND with the mask and check if the bit is 1 or 0.\n(c)Check the conditions:\n(i)If a_bit and b_bit are both 0 and c_bit is 1, it means a flip is required to set the i-th bit in c. Increment flips by 1.\n(ii)If a_bit and b_bit are both 1 and c_bit is 0, it means both bits need to be flipped to 0. Increment flips by 2.\n(iii)If a_bit and b_bit are different (either 0 and 1 or 1 and 0) and c_bit is 0, it means one of the bits needs to be flipped to 1. Increment flips by 1.\n4.Return the final value of flips, which represents the minimum number of flips required.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(log(max(a,b,c))):The time complexity of this approach is O(log(max(a, b, c))) since we iterate over the bits of the largest number among a, b, and c.\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(1):The space complexity is O(1) since we only use a constant amount of extra space to store variables flips, bit, a_bit, b_bit, and c_bit.\n\n# Code\n```\nclass Solution {\npublic:\n    int minFlips(int a, int b, int c) {\n  int flips = 0;\n    //int mask = 1;\n\n     for(int i = 0; (1 << i) <= max(max(a, b), c); i++)\n     {\n            int bit = 1 << i;\n            int a_bit = a & bit ? 1 : 0;\n            int b_bit = b & bit ? 1 : 0;\n            int c_bit = c & bit ? 1 : 0;\n            if(!(a_bit | b_bit) && c_bit)\n            {\n                flips++;\n            }\n            else if((a_bit & b_bit) && !c_bit)\n            {\n                flips+=2;\n            }else if((a_bit ^ b_bit) && !c_bit)\n            {\n                flips++;\n            }\n        }\n        \n        return flips;\n    }\n};\n```
['Bit Manipulation', 'C++']