[C++] Math based 3 line no loop
Math approach\nThe sum of a number and it\'s complement is all 111\'s. For example :\n4 + 3 = 7 (100 + 011= 111)\nNow if we want to find complement we can simpl
# Math approach\nThe sum of a number and it\'s complement is all 111\'s. For example :\n4 + 3 = 7 (100 + 011= 111)\nNow if we want to find complement we can simply subract our number from a number which has all 1\'s from Most significant bit (MSB) to Least significant bit (LSB).\nTo find the most significant bit of a binary number we can simply take log2() of the number. We need to construct a number such that it\'s binary is all 1\'s starting from the most significant bit to least of the number for which we are finding the complement. Consider this example:\n4 in binary is 100\nIn order to get compliment we have to get 111 (all 1\'s from Most significant bit to least) and subract 100 (our number):\n111-100=011 (3)-> Answer\nlog2(4) is 2 in order to get all 1\'s from MSB to LSB we can raise 2 to one power higher than Log2(our number) which would result in 8 for our example and subract 1 from it. Eg:\n2^(log2(4)+1)= 8 in Binary is 1000\nSubtract 1 from it 1000-1 = 0111 (Hence all 1\'s from MSB to LSB)\nNow to get compliment we just have to subtract our number from this and return it.\n```cpp\nclass Solution {\npublic:\n    int bitwiseComplement(int N)\n    {\n        if(N==0)return 1; //As Log(0) is -ve Infinity\n        //calculate left-most-1 in binary number\n        //create Binary number with all ones and subract N from it\n        int sum = (int)pow(2, 1+(int)log2(N))-1;\n        return sum - N;\n    }\n};\n```
['Math', 'C', 'C++']