Python | Recursive solution | Thought Process
Prior Knowledge\n\nYou guys should check out the top posts to understand the general solution of:\n\n\ndef getSum(a, b):\n\tif b == 0: return a\n\treturn getSum
**Prior Knowledge**\n\nYou guys should check out the top posts to understand the general solution of:\n\n```\ndef getSum(a, b):\n\tif b == 0: return a\n\treturn getSum(a ^ b, (a & b) << 1)\n```\n\nBut here\'s a quick summary:\n- Essentially, `a ^ b` add\'s binary numbers that do not require us to "carry" a "bit".\n- The (a & b) finds which bits need to be carried, and we simply bit shift to the left so the carried bit is in the right position. \n- By recursively calling `getSum` again, we will add the previous `a^b` result with the "carry" that needs to be accounted for to complete the add.\n\n**Python solution explanation**\n\nHowever,  in python, integers can exceed 32 bits. This causes a problem for us! So to mitigate this, we have to:\n1. Make sure our integers are strictly 32 bits\n2. Juggle between 32 bit, two\'s complements.\n\nFirst, lets talk about the `mask` variable. Notice that it is equal to `0b11111111111111111111111111111111`. This is simply 32 bits of one\'s. This means any time the integer overflows to more than 32 bits, we do a `& mask` to set any bit greater than 32 to zero. This satifies our constraint #1 of making sure our intergers are strictly 32 bits.\n\nSecond, let\'s deal with the `MAX` variable. To understand the `MAX` variable, you must understand what is [two\'s complement](https://stackoverflow.com/questions/1049722/what-is-2s-complement).  Ultimately, `MAX = 0b01111111111111111111111111111111` is the largest 32 bit integer that is "positve" under the "two\'s compelment" scheme. Thus, when we do `if a <= MAX` we are checking if `a` is positive. If `a` is greater than `MAX`, we have a "negative" number. *However, since python can deal with integers greater than 32 bits, python assumes this negative integer is positive*. Thus, `~(a ^ mask)` allows us to juggle from the positive number to the negative complement, ensuring we return the correct answer.\n\n```\nclass Solution:\n    def getSum(self, a: int, b: int) -> int:\n\n        mask = 0b11111111111111111111111111111111       \n        MAX =  0b01111111111111111111111111111111\n        \n        if b == 0:\n            return a if a <= MAX else ~(a ^ mask)\n        \n        return self.getSum(\n            (a ^ b) & mask,\n            ((a & b) << 1) & mask\n        )\n```\n\nI hope this post has helped explain the python solution a tiny bit better!\n
['Python', 'Python3']