proposed solution for add 2 integers with max bit length

This commit is contained in:
Ivan 2017-07-27 23:58:16 +08:00
parent 8f728dd52d
commit ba6c83fca9

View File

@ -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"
]
},
{