Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add two numbers without the + or - sign [Bug] #205

Open
pupipipu opened this issue Jul 27, 2017 · 0 comments
Open

Add two numbers without the + or - sign [Bug] #205

pupipipu opened this issue Jul 27, 2017 · 0 comments

Comments

@pupipipu
Copy link

The bitwise add 2 integers solution will hit infinite loop for negative integers. The carry will keep increasing if the bit length is not bounded.
Proposed solution:

	def sum_two(self,a,b):
                #max bit length, change to 32 for 32bit
		max_positive = 2 ** 64
		min_negative = -2 ** 64

		while b != 0:
			if b == max_positive:
				return a ^ min_negative
			a,b = a ^ b,(a&b)<<1
		return a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants