Timeline for Sum of Two Integers without using "+" operator in python
Current License: CC BY-SA 3.0
12 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Nov 7, 2021 at 3:41 | comment | added | Matt Messersmith |
@AlexKosh If you're talking about the portion where I remove the mask, that's for a demonstration only to show why the % MASK portion is necessary. Without it, not only does it not obey an int32 type, it may also infinite loop. It's not my "solution". The question is asking to explain what's going on in the OPs solution.
|
|
Nov 7, 2021 at 3:32 | comment | added | Matt Messersmith |
@AlexKosh, getSum(-1,1) (after removing self ) is working fine on Python 3.9.7 on my mac. Did you copy/paste and ensure you don't have typos?
|
|
Nov 6, 2021 at 22:52 | comment | added | Alex Kosh |
I'm getting infinite loop with inputs -1 and 1 . Python version is Python 3.9.0 (tags/v3.9.0:9cf6752, Oct 5 2020, 15:34:40) [MSC v.1927 64 bit (AMD64)] on win32
|
|
Dec 17, 2019 at 19:04 | comment | added | sdr2002 |
2. The function works (perhaps only) when the integer is represented as complement notation (stackoverflow.com/questions/26315782/…). Essentially, -a is written as ~a + 1 . For instance, -1 = 1|1111111 while +1 = 0|0000001 and hence (-1) + (+1) = 0|0000000 . Lastly, you can also easily show that (-1)+(-1) = 1|1111110=-2 which allows the function to deal with every negative numbers.
|
|
Dec 17, 2019 at 18:58 | comment | added | sdr2002 |
1. Loop: Sum of a and b keeps constant over iterations - ^ keeps all the (1,0) pairs and & << keeps the (1,1) pairs. Further, the loop gradually reduces all the 1 bits from b to a at & operation - any (a=0,b=1) occasion drops 1 bit from b - which is always the case for positive numbers.
|
|
Dec 17, 2019 at 18:51 | comment | added | sdr2002 | For the completeness of this thread, I would like to point out two things about the algorithm and interpretation: loop inside the function and mechanism running on for the negative numbers. This is a comment and cannot contain codes nor expressions, so details will be skipped. | |
Dec 17, 2019 at 8:36 | review | Suggested edits | |||
Dec 17, 2019 at 8:50 | |||||
Dec 17, 2019 at 8:29 | review | Suggested edits | |||
Dec 17, 2019 at 8:36 | |||||
Aug 2, 2017 at 7:18 | vote | accept | user1269298 | ||
S Nov 7, 2016 at 10:50 | history | suggested | Elsa Lin | CC BY-SA 3.0 |
I think there are some typos in this post. or --> xor
|
Nov 7, 2016 at 9:03 | review | Suggested edits | |||
S Nov 7, 2016 at 10:50 | |||||
Jul 25, 2016 at 1:24 | history | answered | Matt Messersmith | CC BY-SA 3.0 |