Java | 1 liner | 0 ms | Explained
Idea: Half Adder\n Using HA logic, add a and b bitwise\n sum(a, b) = a XOR b and carry generated = a AND b\n Add this sum to the carry to get the total sum.\n D
**Idea:** [Half Adder](https://en.wikipedia.org/wiki/Adder_(electronics)#Half_adder)\n* Using HA logic, add a and b bitwise\n* sum(a, b) = a `XOR` b and carry generated = a `AND` b\n* Add this sum to the carry to get the total sum.\n* Do this recursively till carry reduces to 0\n> **T/S:** O(1)/O(1)\n\n**Variation 1:** Recursive\n```\npublic int getSum(int a, int b) {\n\treturn (b == 0) ? a : getSum(a ^ b, (a & b) << 1);\n}\n```\n\n**Variation 2:** Iterative\n```\npublic int getSum(int a, int b) {\n\twhile (b != 0) {\n\t\tvar carry = a & b;\n\t\ta ^= b;\n\t\tb = carry << 1;\n\t}\n\treturn a;\n}\n```\n***Please upvote if this helps***
['Recursion', 'Iterator', 'Java']