[C++] Bit Manipulation | Mostly Beginner Friendly | TC: O(1) | 0ms
Intuition\n\nPre-Requisites: Binary Representation && Bit Operations\n\nTo understand the intuition for this problem you need to know the about the binary repre
# Intuition\n\nPre-Requisites: **Binary Representation** && **Bit Operations**\n\nTo understand the intuition for this problem you need to know the about the binary representations of integers and Bit operations that can be performed on them.\nWe need to compare the OR of individual bits `a` and `b` with the corresponding bits of `c`. \nFor the $$i^{th}$$ bit,\nif $$c_i==0$$ then $$a_i$$ and $$b_i$$ should both be zero, if not, we have to flip the bits that are `1`(set).\nif $$c_i==1$$ then either $$a_i$$ or $$b_i$$ should be `1`(set), if both are `0`(unset), we have to flip only one of the two bits.\n\n# Approach\n1. Find the LSB of `a`, `b`, and `c`. (least significant bit). (using $$num \\space & \\space 1$$).\n2. If LSBc is 1, and neither LSBa or LSBb are 1, then increment number of flips by 1.\n3. If LSBc is 0, then add LSBa and LSBb value to number of flips. (We do this because if LSBa is 1, then we need to flip that, and same for LSBb. So if both are 1, then we need to flip both and we add `1+1` to number of flips)\n4. Right shift `a`, `b`, and `c` by `1` bit, so that we can perform the same checks for the next bits.\n5. Repeat steps 1 to 4, while either of `a`, `b`, or `c` are greater than `0`.\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    int minFlips(int a, int b, int c) {\n        int res{0};\n        while(a || b || c) {\n            if ((c&1) == 1)\n                res += ((a&1)==0)&&((b&1)==0);\n            else\n                res += (a&1) + (b&1);\n            a>>=1; b>>=1; c>>=1;\n        }\n        return res;\n    }\n};\n```\n\n# Complexity\n- Time complexity: $$O(32) \\approx O(1)$$\nSince the number is 32-bit, and in worst case we check all bits, the time it\'ll take is $$O(32)$$, which is basically constant.\n\n- Space complexity: $$O(1)$$\nNot using any kind of extra space anywhere.\n\n> ### Note\n> - Upvote if you liked my solution\n> - Drop a comment if you have any doubt
['Bit Manipulation', 'C++']