Easy C solution with explanation beats 100% user in speed||Number Complement
Intuition\n Describe your first thoughts on how to solve this problem. \nAs usual here\'s the proof of the speed: \n\nThe idea is to create a bit mask made of 1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nAs usual here\'s the proof of the speed: \n![image.png](https://assets.leetcode.com/users/images/77f3c969-f0ff-4bec-88c5-76a98ca5e2f4_1724313803.656143.png)\nThe idea is to create a bit mask made of 1 long as the bit representation of num then use the XOR operation and invert the bit. \n# Approach\n<!-- Describe your approach to solving the problem. -->\nTo do what we tought we have to create a mask:\n```\n   int mask=0;\n```\nThen create a cycle that check when the number of bit in "num" is finished:\n```\n    while((num & ~mask) != 0){\n        mask = (mask << 1) | 1;\n    }\n```\nActually I tried to create the right condiction of this cycle but I fixed my errors using Chatgpt but here\'s my explanation: the idea is simple until the logic operation "&" continues to give result 1 it means that the bit is not yet finished, because the operation "&" is the operation bit per bit so: \n```\nnum=5 -----> 00000101\nmask=0 ----> 00000000\n 1) first cycle:\n    00000101 & !(00000000) = 00000101 & 11111111 --> 5!=0 \n    num=5 ------> 00000101\n    mask=0 -----> 00000001\n 2) second cycle:\n    00000101 & !(00000001) = 00000101 & 11111110 --> 4!=0 \n    num=5 ------> 00000101\n    mask=0 -----> 00000011    \n 3) third cycle:\n    00000101 & !(00000011) = 00000101 & 11111100 --> 4!=0 \n    num=5 ------> 00000101\n    mask=0 -----> 00000111 \n 4) fourth cycle: \n    00000101 & !(00000011) = 00000101 & 11111000 --> 0==0\nThe cycle is done!\n```\nAfter this simulation handmade I hope the functioning of the cycle is clear. The last step is to use the XOR operation to return the value researched and why the XOR? Beacause his table of truth is the only one that return 1 when 2 bit are opposite and 0 when they\'re equal:\n```\nTable of truth:\n   | XOR | A | B |\n   |  0  | 0 | 0 |\n   |  1  | 1 | 0 |\n   |  1  | 0 | 1 |\n   |  0  | 1 | 1 |\n```\nSo we return: \n```\n return num ^ mask;\n```\nAnd we\'re done, please upvote tu support me :)\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# Code\n```c []\nint findComplement(int num) {\n    int mask=0;\n    while((num & ~mask) != 0){\n        mask = (mask << 1) | 1;\n    }\n    return num ^ mask;\n}\n```
['Bit Manipulation', 'C']