sum_of_two_integers
1# @leet start 2class Solution: 3 def getSum(self, a: int, b: int) -> int: 4 """ 5 To add two numbers without using the plus operator, we can go back 6 to computer architecture class. 7 To add two bits together, we can use AND and XOR, and a 3rd bit, which 8 is the carry bit, which denotes if we need to add an additional 1 to our 9 result. 10 If we add 0 and 0 and have a carry bit, we get 1 and dont set the carry. 11 If we add 0 and 0 and no carry bit, we get 0 and dont set the carry. 12 If we add 0 and 1 and have a carry bit, we get 0 and set the carry. 13 If we add 0 and 1 and no carry bit, we get 1 and dont set the carry. 14 If we add 1 and 1 and have a carry, we get 0 and set the carry. 15 If we add 1 and 1 and no carry bit, we get 0 and set the carry. 16 17 There are two duplicates (the flip of 1, 0 with and without the carry). 18 But this table sets up our input, we just want to follow the output. 19 To calculate the carry bit, we can AND the two numbers together. 20 To calculate the result, we can XOR the two numbers. 21 We then continue onto the next digit in the bitstring. 22 """ 23 mask = 0xFFFFFFFF 24 while b & mask: 25 carry = (a & b) << 1 26 a = a ^ b 27 b = carry 28 29 return a & mask if b else a 30 31 32# @leet end 33 34 35def test(): 36 assert 2 + 2 == 4
3class Solution: 4 def getSum(self, a: int, b: int) -> int: 5 """ 6 To add two numbers without using the plus operator, we can go back 7 to computer architecture class. 8 To add two bits together, we can use AND and XOR, and a 3rd bit, which 9 is the carry bit, which denotes if we need to add an additional 1 to our 10 result. 11 If we add 0 and 0 and have a carry bit, we get 1 and dont set the carry. 12 If we add 0 and 0 and no carry bit, we get 0 and dont set the carry. 13 If we add 0 and 1 and have a carry bit, we get 0 and set the carry. 14 If we add 0 and 1 and no carry bit, we get 1 and dont set the carry. 15 If we add 1 and 1 and have a carry, we get 0 and set the carry. 16 If we add 1 and 1 and no carry bit, we get 0 and set the carry. 17 18 There are two duplicates (the flip of 1, 0 with and without the carry). 19 But this table sets up our input, we just want to follow the output. 20 To calculate the carry bit, we can AND the two numbers together. 21 To calculate the result, we can XOR the two numbers. 22 We then continue onto the next digit in the bitstring. 23 """ 24 mask = 0xFFFFFFFF 25 while b & mask: 26 carry = (a & b) << 1 27 a = a ^ b 28 b = carry 29 30 return a & mask if b else a
4 def getSum(self, a: int, b: int) -> int: 5 """ 6 To add two numbers without using the plus operator, we can go back 7 to computer architecture class. 8 To add two bits together, we can use AND and XOR, and a 3rd bit, which 9 is the carry bit, which denotes if we need to add an additional 1 to our 10 result. 11 If we add 0 and 0 and have a carry bit, we get 1 and dont set the carry. 12 If we add 0 and 0 and no carry bit, we get 0 and dont set the carry. 13 If we add 0 and 1 and have a carry bit, we get 0 and set the carry. 14 If we add 0 and 1 and no carry bit, we get 1 and dont set the carry. 15 If we add 1 and 1 and have a carry, we get 0 and set the carry. 16 If we add 1 and 1 and no carry bit, we get 0 and set the carry. 17 18 There are two duplicates (the flip of 1, 0 with and without the carry). 19 But this table sets up our input, we just want to follow the output. 20 To calculate the carry bit, we can AND the two numbers together. 21 To calculate the result, we can XOR the two numbers. 22 We then continue onto the next digit in the bitstring. 23 """ 24 mask = 0xFFFFFFFF 25 while b & mask: 26 carry = (a & b) << 1 27 a = a ^ b 28 b = carry 29 30 return a & mask if b else a
To add two numbers without using the plus operator, we can go back to computer architecture class. To add two bits together, we can use AND and XOR, and a 3rd bit, which is the carry bit, which denotes if we need to add an additional 1 to our result. If we add 0 and 0 and have a carry bit, we get 1 and dont set the carry. If we add 0 and 0 and no carry bit, we get 0 and dont set the carry. If we add 0 and 1 and have a carry bit, we get 0 and set the carry. If we add 0 and 1 and no carry bit, we get 1 and dont set the carry. If we add 1 and 1 and have a carry, we get 0 and set the carry. If we add 1 and 1 and no carry bit, we get 0 and set the carry.
There are two duplicates (the flip of 1, 0 with and without the carry). But this table sets up our input, we just want to follow the output. To calculate the carry bit, we can AND the two numbers together. To calculate the result, we can XOR the two numbers. We then continue onto the next digit in the bitstring.