Two Lines of code and two Approaches (Intuition Explained)
Approach-1 using power And log\n# Intuition\n Describe your first thoughts on how to solve this problem. \nBy the exponent product rule in mathematics,\n\n x^(a
# Approach-1 using power And log\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nBy the exponent product rule in mathematics,\n\n x^(a+b) = x^a * x^b.\n\n Here we can get the sum of a and b without using + operator. \n\na+b = logx(x^(a+b)) = logx(x^a * x^b)\n\nHere we can use any number for x and I\'ve used 2 for keeping it small. \n\nfinal line - a+b = log2(2^(a+b)) = log2(2^a * 2^b)\n\n(in Java to take log base 2, i think there\'s no straight way to do it so we take log of a number then divide it by log of 2 . It\'s one of the log property in mathematics)\n\n# Code\n```\nclass Solution {\n    public int getSum(int a, int b) {\n        if(b==0 || a==0) return a | b;\n        return (int)(Math.log(Math.pow(2,a)*Math.pow(2,b))/Math.log(2));\n    }\n}\n```\n\n\n``\n# Approach-2 using bitwise XOR and AND\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe first approach won\'t be good for big numbers.\nIn this approach we take\n XOR to take the dissimilar bits or the sum without carry and \nAND to take the similar bits or the sum with carry and we left shift it by one to make the carry bits go to their places. \nWe do this recursively until we get either one of the operations as zero.\nLet\'s take an example to illustrate this approach for adding two binary numbers. Suppose we want to add the binary numbers 1101 and 1011.\n\nFirst, we perform the XOR operation between the two numbers:\n\n  1101\n  1011 ^\n  ----\n  0110\n\nThe result of the XOR operation is 0110, which represents the sum of the two binary numbers without the carry.\n\nNext, we perform the AND operation between the two numbers:\n\n  1101\n  1011 &\n  ----\n  1001\n\nThe result of the AND operation is 1001, which represents the positions where there is a carry.\n\nWe left shift the carry by one to make the carry bits go to their places:\n\n   1001\n   1    <<\n  ----\n  10010\n\nThe result of the left shift is 10010, which represents the carry bits in their proper place.\n\nNow, we can repeat the same process using the sum without carry and the carry bits. We add the sum without carry and the carry bits:\n\n   0110\n+ 10010\n  ------\n  10100\n\nThe result of this addition is 10100, which represents the final sum of the two binary numbers.\n\nWe can continue this process recursively until either the XOR or the AND operation becomes zero. This approach works well for small binary numbers, but for big binary numbers, the number of recursive calls required can be very large, which makes this approach inefficient.\n\n# Code\n```\nclass Solution {\n    public int getSum(int a, int b) {\n        if(b==0 || a==0) return a | b;\n        return getSum(a^b, (a&b)<<1);\n    }\n}\n```
['Java']