Kotlin. Beats 100% (112 ms). Calculate mask then xor.
\n\n# Code\nkotlin []\nclass Solution {\n    fun findComplement(num: Int): Int {\n        var n = num\n        var mask = 0\n        while (n != 0) {\n         
![image.png](https://assets.leetcode.com/users/images/88f7af03-8085-48b9-a76d-cace91b46c59_1724314389.2553186.png)\n\n# Code\n```kotlin []\nclass Solution {\n    fun findComplement(num: Int): Int {\n        var n = num\n        var mask = 0\n        while (n != 0) {\n            n = n shr 1\n            mask = mask shl 1 or 1\n        }\n        return num xor mask\n    }\n}\n```\n\n# Approach\n\nThe  algorithm is simple - we must first calculate a mask of ones for all the bits that are included in the given number. For example, for the number 5 (101b) this will be a mask of 7 (111b).\n\nThe next step is to XOR the mask and the number so that the result will be bits that are not included in the initial number:\n\n```\n111 x 101 = 010\n```\n\n010b = 2\n\n# Alternative approaches\n\n1. "Integer.takeHighestOneBit".\n\nAlternatively, we can use the "Integer.takeHighestOneBit()" operation with a pre-computed set of masks for all 32 variants, but it is slower, because the "Integer.takeHighestOneBit()" operation does a lot of calculations internally (bitwise shifts).\n\n![image.png](https://assets.leetcode.com/users/images/720f9328-d269-4102-bf92-43a9a56ab379_1724315876.705344.png)\n\n```\nclass Solution {\n    fun findComplement(num: Int): Int {\n        return num xor (masks[num.takeHighestOneBit()] ?: 0)\n    }\n\n    companion object {\n        val masks = TreeMap<Int, Int>()\n\n        init {\n            for (i in 0..32) {\n                val bit = 1 shl i\n                masks.put(bit, (bit shl 1) - 1)\n            }\n        }\n    }\n}\n```\n\n2. Direct xoring \n\n![image.png](https://assets.leetcode.com/users/images/2d8d1fae-8b44-4002-9bb4-dfeae013b2d7_1724316755.4818318.png)\n\n```\nclass Solution {\n    fun findComplement(num: Int): Int {\n        var n = num\n\n        var result = 0\n        var offset = 0\n        for (i in 0 until 31) {\n            if (n == 0) break\n            val bit = (n and 1) xor 1\n            result = result or (bit shl offset)\n            offset++\n            n = n shr 1\n        }\n\n        return result\n    }\n}\n```\n\n3. Direct xoring #2\n\n![image.png](https://assets.leetcode.com/users/images/22b0bcb8-52b6-465c-a808-ae1159f40b84_1724316979.7803967.png)\n\n```\nclass Solution {\n    fun findComplement(num: Int): Int {\n        var n = num\n\n        var result = 0\n        for (i in 0 until 31) {\n            if (n == 0) break\n            result = result or ((1 shl i xor num) and (1 shl i))\n            n = n shr 1\n        }\n\n        return result\n    }\n}\n```\n\n4. Direct xoring #3\n\nIt is also worth noting that we do not use the 31st bit, since it is the sign bit.\n\n![image.png](https://assets.leetcode.com/users/images/faecf1ca-7fe2-4f79-b066-5a8f42d94365_1724317734.227085.png)\n\n```\nclass Solution {\n    fun findComplement(num: Int): Int {\n        var result = 0\n        for (i in 0 until 31) {\n            val bit = 1 shl i\n            if (bit > num) break\n            result = result or ((num and bit) xor bit)\n        }\n        return result\n    }\n}\n```
['Kotlin']