Well Explained JAVA & C++ || 2 Approaches || Easy for mind to Accept it
Intitution\n\nWe have to convert 5 -----> 101 -----> 010 ------> 2\nSteps:                   1           2           3\n\n\n\nMethod - 1 :\nApproach to use:\n\n
**Intitution**\n```\nWe have to convert 5 -----> 101 -----> 010 ------> 2\nSteps:                   1           2           3\n```\n\n\n**Method - 1 :**\n**Approach to use:**\n```\nNow we take modulo of 5 i.e.  % 2 ----> 1     ^\n\t\t\t\t\t\t\t\t2 ----> 0     |\n\t\t\t\t\t\t\t\t1 ----> 1     |\n\t\tand finally it become\'s 0 \n\nWhat I did is we take modulo of 5 i.e. 5 % 2 = 1 ;then divide by 2 i.e         5 / 2 = 2;\nagain modulo of 2 i.e                  2 % 2 = 0 ;then again we divide by 2    2 / 2 = 1;\n\n\ngoing from down to top, we will get 101, now by compliment we can convert 101 to 010\n\nNow for 010, taking least significant digit. we go from right to left\ni.e. 0 * 2^0\n    +1 * 2^1\n    +0 * 2^2\n    ---------\n    =   2\n```\n**Java**\n```\nclass Solution {\n    public int bitwiseComplement(int n) {\n        if(n == 0) return 1; // Checking for base case\n        int res = 0;\n        int fac = 1; // keep for 2 basically\n        \n        while(n != 0){\n            // first we need to check what is our bit in 2 by taking modulo\n            res += fac * (n % 2 == 0 ? 1 : 0);\n            // res is the number convert back to decimal + factor * n % 2 if comes 0 then we take 1 otherwise 0 this is our complement\n            \n            fac *= 2;\n            n /= 2;\n        }\n        return res;\n    }\n}\n```\n**C++**\n```\nclass Solution {\npublic:\n    int bitwiseComplement(int n) {\n        if(n == 0) return 1; // Checking for base case\n        int res = 0;\n        int fac = 1; // keep for 2 basically\n        \n        while(n != 0){\n            // first we need to check what is our bit in 2 by taking modulo\n            res += fac * (n % 2 == 0 ? 1 : 0);\n            // res is the number convert back to decimal + factor * n % 2 if comes 0 then we take 1 otherwise 0 this is our complement\n            \n            fac *= 2;\n            n /= 2;\n        }\n        return res;\n    }\n};\n```\nANALYSIS:-\n* **Time Complexity :-** BigO(logN)\n\n* **Space Complexity :-** BigO(1)\n![image](https://assets.leetcode.com/users/images/55231346-ad58-4695-9b4c-98e457c0b9a2_1641273713.176929.png)\n**Method - 2 :**\n**Approach to use:**\n```\n5 -> 101 ^ 010 -> 111 // taking XOR of it\'s no and complement we get 111\n\n10 - > 1010 ^ 0101 -> 1111\n\nSo, looking at above example we can say that,\n                    A ^ B = C\nthen,                    \n                    A ^ A ^ B = A ^ C\nbut                 A ^ A is 0;\ntherefore,          0 ^ B is B;\nHence we can say    B = A ^ C\n\nSo, these property of XOR apply in our case\n\nLet say our no is "N" and no we have to find is "X"\nlet say N ^ X gives [111....] something,\nwhere 111 is length of our N\nand X = [1111..] length is same as binary representation of N\n\nSo, from this the formula we get is :\n        -----------------\n        X = [1111...] ^ N\n        -----------------\n        \nBut here thing is how we will get [1111...], in java we have method by taking Integer.toBinaryString(). \nAfter taking length we will do first bitwise left shift of 1\ni.e  1 << 3  gives   1000\nand if i subtract      -1\ngives                 111\n\nthis is what i need in case of N = 5, where the length of my binary representation is 3\n\nSo, that\'s how we gonna do it.\n```\n**JAVA**\n```\nclass Solution {\n    public int bitwiseComplement(int n) {\n        return n == 0 ? 1 : ((1 << Integer.toBinaryString(n).length()) - 1) ^ n;\n    }\n}\n```\n**C++**\n```\nclass Solution {\npublic:\n    int bitwiseComplement(int n) {\n        return n>0? (int)((1ll<<((int)log2(n)+1))-1)^n : 1;\n    }\n};\n\n```\nANALYSIS:-\n* **Time Complexity :-** BigO(logN)\n\n* **Space Complexity :-** BigO(1)\n\n*Guys, If you downvote. Please do let me know in comment section. What\'s wrong with the code or explanation. So, that I can correct and improve it. \nThanks (:*
['C', 'Java']