Easy Python Solution 🐍
Intuition\n Describe your first thoughts on how to solve this problem. \n\nThe code is a Python function implemented as a method of a class called Solution. Thi
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\nThe code is a Python function implemented as a method of a class called Solution. This function takes three integer inputs a, b, and c and returns an integer which represents the minimum number of bit flips required to make a | b equal to c.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n- ab is a binary number obtained by performing the bitwise OR operation between a and b. This operation sets each bit in ab to 1 if that bit is set in either a or b.\n\n- equal is a binary number obtained by performing the bitwise XOR operation between ab and c. This operation sets each bit in equal to 1 if the corresponding bits in ab and c are different.\n\n- ans is a counter variable that keeps track of the minimum number of bit flips required.\n\nThe code then iterates over each bit position (0 to 30) and checks if the bit at that position needs to be flipped. If the bit needs to be flipped, depending on the values of a, b, and c, it increments ans by either 1 or 2.\n\nThe condition equal & mask > 0 checks if the bit at position i needs to be flipped. If this condition is true, then one of two things can happen:\n\n- If the bits in a, b, and c are such that flipping the bit at position i in both a and b makes a | b equal to c, then ans is incremented by 2. This is because we need to flip both bits in a and b to get from ab to c.\n\n- If the bits in a, b, and c are such that flipping the bit at position i in only one of a or b (or neither) makes a | b equal to c, then ans is incremented by 1. This is because we only need to flip one bit to get from ab to c.\n\n# Complexity\n- Time complexity : $$O(log n)$$\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# Code\n```\nclass Solution:\n    def minFlips(self, a: int, b: int, c: int) -> int:\n        ab, equal, ans = a | b, (a | b) ^ c, 0\n        for i in range(31):\n            mask = 1 << i\n            if equal & mask > 0:\n                if (a & mask) == (b & mask) and (c & mask) == 0:\n                    ans += 2 \n                else:\n                    ans += 1\n        return ans\n\n```\n\n\n![e2515d84-99cf-4499-80fb-fe458e1bbae2_1678932606.8004954.png](https://assets.leetcode.com/users/images/a5125831-8710-4929-946c-38eebae827b3_1686150303.8650544.png)\n
['Bit Manipulation', 'Python', 'Python3']