Short & Fast Java Solution (w/ Explanation)
Algorithm:\n\npublic int findComplement(int num) {\n\tint mask = -1;\n\twhile ((num & mask) != 0) {\n\t\tmask <<= 1;\n\t}\n\treturn ~num & ~mask; // Also equal 
**Algorithm:**\n```\npublic int findComplement(int num) {\n\tint mask = -1;\n\twhile ((num & mask) != 0) {\n\t\tmask <<= 1;\n\t}\n\treturn ~num & ~mask; // Also equal to ~(num ^ mask)\n}\n```\n**Explanation:**\nLets take this step by step. For the sake of my sanity, lets assume that  integers are only 8-bits long. In our example, we will say that the bit representation of num is `00001101`.\n1. This sets mask to be an integer with ALL of its bits set to 1.\n\n\t```\n\tint mask = -1;\n\t```\n\tNote the current state of the variables:\n\t```\n\tnum:         00001101\n\tmask:        11111111\n\t```\n2. This while condition shifts the mask until it lines up with the leading zeroes in num. \n\t```\n\twhile ((num & mask) != 0) {\n\t\tmask <<= 1;\n\t}\n\t```\n\tNote that shifting the mask left will drop the first bit, and append a 0 to the end. (`11111111` shifted  once would be `11111110`). Also, it is only once there are no more 1s in num that their bitwise-and would equal 0. Here is the state after this code is run:\n\t```\n\tnum:         00001101\n\tmask:        11110000\n\tnum & mask:  00000000\n\t```\n3. Finally, we need to invert the bits of num and only return the bits that were not part of the leading zeroes.\n\t```\n\treturn ~num & ~mask;\n\t```\n\tWe invert (flip 1s to 0s and vice versa) num because that is the thing we want to return. However, the leading zeroes need to be factored out, so we invert the mask and "cancel" out the leading zeroes. Note the following:\n\t```\n\t~num:         11110010\n\t~mask:        00001111\n\t~num & ~mask: 00000010\n\t```\nSo, we end up returning the correct value of `00000010`.\n\nLet me know if anything does not make sense!
['Bit Manipulation', 'Java']