Python Simple Solution with Detail Explanation : O(log n)
There are 3 steps to be performed in this question:\n1. Convert Decimal (Base 10) number into to binary (Base 2)\n\t(We divide the number by 2 and write the rem
There are 3 steps to be performed in this question:\n1. Convert Decimal (Base 10) number into to binary (Base 2)\n\t(We divide the number by `2` and write the remainders from bottom to top order)\n\t**Example:**\n\t![image](https://assets.leetcode.com/users/images/571a9862-382b-46be-bc26-75d55c7a466f_1641805033.2479856.png)\n(Picture credit gfg)\n\n2. Take the complement of the binary number\n\t(Convert all **0 -> 1** and **1 -> 0**)\n\n3. Convert the complement number back into decimal (Base 10)\n\tEvery place in a number has its factors eg: one\'s place(**2^0**), ten\'s place(**2^1**) and so on.\n\t**Example:**\n\t![image](https://assets.leetcode.com/users/images/d2f9aa63-d858-4ae0-93e4-4624c72d0dab_1641804822.7457397.png)\n(Picture credit ncalculators)\t\n\t\n**Base case:**\nIf number (in decimal format) is `0` then it will be `0` (in binary format) and it\'s complement will be `1`(in binary) and converting it again will be `1` (in decimal) \n\tSo we can directly **return `1` if n is `0`**\n\t\n**Else part:**\n    We take 2 variables, `result` which will store the final output and, \n\t`factor` which is basically every place factors **(2^0 = 1, 2^1 = 2, 2^2 = 4)** which is inshort multiplied by `2 `in every step.\n    \n`(1 if n%2 == 0 else 0) `\nThis part is a mixture of step 1 and 2.\n* If `n % 2` is `0` that means in division(step1) the remainder will come `0` and its complement is `1` so we return the complement (which is `1`) \n* And similarly, is `n % 2` is `1` that means in division(step1) the remainder is `1` and its complement is `0` so we return the complement(which is `0`)\n\t\nLet us understand how the code works with an example:\n![image](https://assets.leetcode.com/users/images/bd32d94c-a1db-4671-a3ca-11d3e4acd1db_1641804675.0959334.jpeg)\n\n\n**Code:**\n```\nclass Solution:\n    def bitwiseComplement(self, n: int) -> int:\n        if n == 0:\n            return 1\n        else:\n            result = 0\n            factor = 1\n            \n            while(n > 0):\n                result += factor * (1 if n%2 == 0 else 0)\n                factor *= 2\n                n //= 2\n            return result\n```
['Python', 'Python3']