Bit Manipulation. Beats 100% solutions
Intuition\n Describe your first thoughts on how to solve this problem. \nIf bit value of c at ith position is 1 and a and b at ith position is 0, then change ei
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nIf bit value of c at ith position is 1 and a and b at ith position is 0, then change either the value of a or b at ith position to 1. \nIf bit value of c at ith position is 0, we will change both values of a and b at ith position to 1.\nWe will use loop from 0 to 31 bcz we are considering 32 bit integer.\n\n---\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- Take a counter \'cnt\' to count the flips.\n- if ((c & (1 << i)) && !(a & (1 << i)) && !(b & (1 << i)))  \n  First if condition checks if \'c\' has non-zero at ith position, and \'a\' and \'b\' have 0 at ith position. If \'a\' has 0 value, we change it to 1 or if \'b\' has 0 value we change it to 1. We have to change only one value bcz \'OR\' gives 1 even if one input is 1. So we increase the cnt by 1.\n- if (!(c & (1 << i)) && ((a & (1 << i)) || (b & (1 << i))))\n   Second if condition checks if whether \'c\' has 0 and \'a\' or \'b\' have non-zero value bcz even if one value is non-zero, the result will not be zero. Then we flip the bit of a and b accordingly and increase the cnt.\n- At last, return the cnt.\n\n- Refer OR table for any problem\n    A   B\tA \u2228 B\n    0\t0\t  0\n    0\t1\t  1\n    1\t0\t  1\n    1\t1\t  1\n\nI know you can have more easy code than mine by using boolean variables, to check for 0 and 1. But by my code, you are changing the bit value in the number itself which is I guess more convenient if you are a learner.\n\n---\n# Complexity\n- Time complexity: O(1)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n---\n\n\n# Code\n```C++[]\nclass Solution {\npublic:\n    int minFlips(int a, int b, int c) {\n       int cnt = 0;\n    for (int i = 0; i < 32; i++)\n    {\n        if ((c & (1 << i)) && !(a & (1 << i)) && !(b & (1 << i)))\n        {\n            if (!(a & (1 << i)))\n                a |= (1 << i);\n            else\n                b |= (1 << i);\n            cnt++;\n        }\n        if (!(c & (1 << i)) && ((a & (1 << i)) || (b & (1 << i))))\n        {\n            if ((a & (1 << i)))\n            {\n                a &= ~(1 << i);\n                cnt++;\n            }\n            if ((b & (1 << i)))\n            {\n                b &= ~(1 << i);\n                cnt++;\n            }\n        }\n    }\n    return cnt;\n    }\n};\n```\n```JAVA[]\npublic class Solution {\n    public int minFlips(int a, int b, int c) {\n        int cnt = 0;\n        for (int i = 0; i < 32; i++) {\n            if ((c & (1 << i)) != 0 && (a & (1 << i)) == 0 && (b & (1 << i)) == 0) {\n                if ((a & (1 << i)) == 0)\n                    a |= (1 << i);\n                else\n                    b |= (1 << i);\n                cnt++;\n            }\n            if ((c & (1 << i)) == 0 && ((a & (1 << i)) != 0 || (b & (1 << i)) != 0)) {\n                if ((a & (1 << i)) != 0) {\n                    a &= ~(1 << i);\n                    cnt++;\n                }\n                if ((b & (1 << i)) != 0) {\n                    b &= ~(1 << i);\n                    cnt++;\n                }\n            }\n        }\n        return cnt;\n    }\n\n    public static void main(String[] args) {\n        Solution solution = new Solution();\n        int a = 2, b = 6, c = 5;\n        int result = solution.minFlips(a, b, c);\n        System.out.println("Result: " + result);\n    }\n}\n```\n![Screenshot 2024-03-07 120724.png](https://assets.leetcode.com/users/images/5e6f57c7-655d-416b-b974-a1ba96d93360_1709793717.1267078.png)\n\n\n
['C++', 'Java']