Two approaches. Math and Bit Manipulation. Detailed explanation
Math Approach\nWe will use the formula for the logarithm of the product of two numbers, so we can get our plus.\nln(a * b) = ln(a) + ln(b)\n\nNow we have to get
# Math Approach\nWe will use the formula for the logarithm of the product of two numbers, so we can get our plus.\n$$ln(a * b) = ln(a) + ln(b)$$\n\nNow we have to get the numbers a and b.\nWe can get these numbers from the formula, where instead of n we can substitute a or b.\n$$ln(exp^n) = n*ln(exp) = n$$\n\nAs a result we get the formula\n$$ln(exp^a * exp^b) = ln(exp^a) + ln(exp^b) = a(ln(exp)) + b(ln(exp)) = a + b$$\n\n- Note: any other basis can be used instead of the natural algorithm\n\n# The complexity of both approaches\n- Time: $$O(1)$$\n- Space: $$O(1)$$\n\n# Math code\n```\nclass Solution {\npublic:\n    int getSum(int a, int b) {\n        return log2(std::pow(2, a) * std::pow(2, b));\n    }\n};\n```\n# Bit manipulation approach\nLet\'s look at an example:\n14 - 0 1110\n03 - 0 0011\n17 - 1 0001\n\nBits 0 0, 1 0, 0 1 we can add freely, but there is a problem. For example 1 1 because we need to move the bit to the next digit.\n\nFirst we can trace all such bits with bitwise AND, if we end up with 0 we can use bitwise XOR to add 2 our numbers and return the answer.\n\n1. In our case, we will get 10 (in binary) and write it to the buffer.\n2. We add our 2 numbers 1110 ^ 0011 = 1101\n3. Assign the number b to a buff shift of 1, since we will later sum the buff with our number a. We need this shift in order to add ones to the next most significant digits.\n4. Repeat until there are no more bits to be shifted, i.e. our buffer will contain only zeros.\n\n```\nIteration 1:\nbuff = 1110 & 11 = 10\na = 1110 ^ 0011 = 1101\nb = 100\n\nIteration 2:\nbuff = 1101 & 100 = 100\na = 1101 ^ 100 = 1001\nb = 1000\n\nIteration 3:\nbuff = 1001 & 1000 = 1000\na = 1001 ^ 1000 = 1\nb = 1 0000\n\nIteration 4:\nbuff = 1 & 1 0000 = 0\na = 1 ^ 1 0000 = 1 0001\nb = 0\n\nend: answer 17\n```\n##### Explanation of unsigned int data type\n\nSince the last bit of int is used to determine the sign, the << operation shifts it, which causes undefined compiler behavior.\nSo LeetCode gives an error. unsigned int solves this problem\n\n\n# Bit manipulation code\n```\nclass Solution {\npublic:\n    int getSum(int a, int b) {\n        unsigned int buff;\n        do {\n            buff = a & b;\n            a ^= b;\n            b = buff << 1;\n        } while(buff);\n        return a;\n    }\n};\n```
['C++']