add without carry

Bryan Olson fakeaddress at nowhere.org
Fri Sep 15 07:17:47 EDT 2006


Bruno Desthuilliers wrote:
> Bryan Olson wrote:
>> Hugh wrote:
>>> Sorry, here's an example...
>>>
>>> 5+7=12
>>>
>>> added without carrying, 5+7=2
>>>
>>> i.e the result is always less than 10
>> Are you looking for bitwise exclusive or? In Python it's
>> the '^' operator. For example:
>>
>>     print 5 ^ 7
>>
>>
>>>> 10 ^ 21
> 31
> 
> Not really "less than 10"...

I had little idea what he meant by that. My guess was that
no bit clear in an operand could be set in the result.
XOR is also known as mod-2 addition. It's what you get
if you add each bit independently, dropping carries, so
I thought it might be what he was looking for.
Unfortunately the bitwise mod-2 addition of 5 and 7 gives
the same result as mod-10 addition. Oh well.


-- 
--Bryan



More information about the Python-list mailing list