Easy Bit Manipulation in detail explanation
Intuition\n Describe your first thoughts on how to solve this problem. \nFirst check for feasibility of transformation and then proceed to add the no.of set bit
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nFirst check for feasibility of transformation and then proceed to add the no.of set bits.\n# Approach\n<!-- Describe your approach to solving the problem. -->\n```\nif(n!=(n|k))\n    return -1;\n```\nThis line checks for feasibility.\nIf n and (n | k) are different, it means there are set bits in n that can\'t be turned off to match k, making the transformation impossible. Thus, return -1.\n\n```\nwhile(swap!=0)\n{\n    ans+=swap&1;\n    swap>>=1;\n}\n```\n- n & ~k produces a mask where $each$ $bit$ $is$ $1$. If n has a 1 where k has a 0. This is because ~k flips the bits of k, and n & ~k extracts the bits from n where k has 0\'s.\n- while(swap != 0)loop counts the $number$ $of$ $1s$ $in$ $swap$ (i.e., how many bits need to be changed). It does this by checking the least significant bit $(swap & 1)$, adding it to ans, and then $shifting$ $swap$ $right$ by one position to process the next bit.\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 {\npublic:\n    int minChanges(int n, int k) {\n        if(n!=(n|k))\n            return -1;\n        int swap=n&~k,ans=0;\n        while(swap!=0)\n        {\n            ans+=swap&1;\n            swap>>=1;\n        }\n        return ans;\n    }\n};\n```\n![KeT.jpeg](https://assets.leetcode.com/users/images/60830890-b1ed-48c2-8e29-6ecd73af4204_1721534772.6608024.jpeg)\n
['Bit Manipulation', 'Bitmask', 'C++']