mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
proposed solution for add 2 integers with max bit length
This commit is contained in:
parent
8f728dd52d
commit
ba6c83fca9
|
@ -122,11 +122,14 @@
|
|||
" def sum_two(self, a, b):\n",
|
||||
" if a is None or b is None:\n",
|
||||
" raise TypeError('a or b cannot be None')\n",
|
||||
" result = a ^ b;\n",
|
||||
" carry = (a&b) << 1\n",
|
||||
" if carry != 0:\n",
|
||||
" return self.sum_two(result, carry)\n",
|
||||
" return result;"
|
||||
" #max bit length, change to 32 for 32bit\n",
|
||||
" max_positive = 2 ** 64\n",
|
||||
" min_negative = -2 ** 64\n",
|
||||
" while b != 0:\n",
|
||||
" if b == max_positive:\n",
|
||||
" return a ^ min_negative\n",
|
||||
" a,b = a ^ b,(a&b)<<1\n",
|
||||
" return a"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue
Block a user