Check Least Significant Bit | Python / JS Solution
Hello Tenno Leetcoders, \n\nFor this problem, given 3 positives numbers a, b and c. Return the minimum flips required in some bits of a and b to make ( a OR b =
Hello **Tenno Leetcoders**, \n\nFor this problem, given 3 positives numbers `a`, `b` and `c`. Return the minimum flips required in some bits of `a` and `b` to make `( a OR b == c )`. (bitwise OR operation).\n\nFlip operation consists of change any single bit 1 to 0 or change the bit 0 to 1 in their binary representation.\n\n### Explanation \n\nWe want to find the minimum required flips needed to make `( a OR b == c )`. To do that, we can use bitwise operations. \n\nCreate a variable to keep track of minimum number of flips required.\n\nWe want to iterate through the bits of `a`, `b` and `c` simultaneously and check for the `least significant bit` using bitwise `AND` operation with `1`. The number `1` in binary is represented as `00000001`, and performing the `& 1` operation will extracts the rightmost bit of each number, resulting in `1` if bit is set to `1` and `0` if bit is unset `0`\n\nUsing the least significant bit, we will perform a few checks:\n\n1)  If a & 1 or b & 1 is not equal to c & 1, this means either bit a or bit b is 1 (at least one bit is 1) but c is 0. Thus, we need to perform flips to make `a or b == c`\n\n    - If bit c is equal to 1, this means the current bit in c is 1, and the OR operation will result in 1. So increment tracker variable by 1 to represent the need to flip `a` and `b` to match `c`\n    \n    - If bit c is equal to 0, this means that the current bit in c is 0, and the OR operation will result in 0. If at least one of bit a or bit b is 1, we need to flip the corresponding bit to 0, so increment the tracker variable by the number of bits needed to be flipped, which is ` (bit_a == 1) + (bit_b == 1)`\n    \n2) To move onto the next bit after processing the current bit, we right-shift `a`, `b` and `c` by 1 to discard the rightmost bit and shit all other bits towards the right\n\n3) Loop will continue until a, b and c becomes `0`. Thus, we will return the minimum flips needed \n\n\n# Code\n\n**Python**\n```\nclass Solution:\n    def minFlips(self, a: int, b: int, c: int) -> int:\n        min_flip = 0\n\n        while a or b or c:\n            bit_a = a & 1\n            bit_b = b & 1\n            bit_c = c & 1\n\n            if (bit_a or bit_b) != bit_c:\n                if bit_c == 1: \n                    min_flip += 1\n                else:\n                    min_flip += (bit_a == 1) + (bit_b == 1)\n               \n            a >>= 1\n            b >>= 1\n            c >>= 1\n\n        return min_flip\n```\n\n**JavaScript**\n```\n/**\n * @param {number} a\n * @param {number} b\n * @param {number} c\n * @return {number}\n */\nvar minFlips = function(a, b, c) {\n    let minFlip = 0\n\n    while(a || b || c){\n        const bitA = a & 1\n        const bitB = b & 1\n        const bitC = c & 1\n        if ((bitA || bitB) !== bitC){\n            bitC === 1 ? minFlip += 1 : minFlip += (bitA === 1) + (bitB === 1)\n        }\n        a >>= 1\n        b >>= 1\n        c >>= 1\n    }\n    return minFlip\n};\n```\n### Time Complexity: `O(log(max(a,b,c)))`\n### Space Complexity: `O(1)`\n\n \n***Warframe\'s Darvo wants you to upvote this post  \uD83D\uDE4F\uD83C\uDFFB \u2764\uFE0F\u200D\uD83D\uDD25***\n![image](https://assets.leetcode.com/users/images/814f5668-c966-46d7-ba42-e5435c4c1761_1675302761.3081913.gif)
['Python3', 'JavaScript']