473,395 Members | 1,689 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

add without carry

I would like to perform an addition without carrying of two integers...
I've got no idea how to do this in python, although I've been using it
for web/cgi/db work for a few years now.

Any help would be great.

Hugh

Sep 15 '06 #1
17 5123
In <11**********************@i42g2000cwa.googlegroups .com>, Hugh wrote:
I would like to perform an addition without carrying of two integers...
I've got no idea how to do this in python, although I've been using it
for web/cgi/db work for a few years now.
Your description is a bit vague. What is `without carrying`? Do you want
to limit the result to a given bit length with a "wrap around" behavior if
the number gets too large to fit into those bits? Then this should do the
trick:

a = (b + c) & (2**bits - 1)

Ciao,
Marc 'BlackJack' Rintsch
Sep 15 '06 #2

Hugh wrote:
I would like to perform an addition without carrying of two integers...
I've got no idea how to do this in python, although I've been using it
for web/cgi/db work for a few years now.
In multiword addition in assembly language, one uses a normal ADD
instruction on the lowest-order pair of words, and an ADC (or ADDC or
whatever) i.e add *with* carry which adds in the carry bit from the
previous operation for each subsequent pair of words. IOW, addition
*without* carrying is normal, and I don't recall hearing the expression
before ......

Do you possibly mean: add two 32-bit integers such the result fits in
32 bits i.e. discard the generated carry bit, keep the low-order
32-bits?

If so, that's simply (a + b) & 0xFFFFFFFF (in Python as well as a few
other languages).

If you meant a different number of bits, well the answer for 8 bits
would be (a + b) & 0xFF.

Otherwise, you need to provide some examples of two integers and what
"addition without carrying" produces.

HTH,
John

Sep 15 '06 #3
Sorry, here's an example...

5+7=12

added without carrying, 5+7=2

i.e the result is always less than 10

I've been thinking some more about this and my brain is starting to
work something out... I just wondered if there was a function in python
math to do this automatically...

Hugh

Sep 15 '06 #4
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
def add(a, b, c=10):
an = a + b
if an >= c:
an -= c
return an

add(5, 7) # = 2

?

Regards,
Jordan

Sep 15 '06 #5
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
--
--Bryan
Sep 15 '06 #6
Thankyou everyone this gives me something to work with.

Hugh

Sep 15 '06 #7
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

I've been thinking some more about this and my brain is starting to
work something out... I just wondered if there was a function in python
math to do this automatically...
>>(5 + 7) % 10
2

In this context '%' is called 'modulo operator'. What John and Marc
suggested is basically the same operation for the special case of binary
numbers, i. e.

a % b == a & (b-1)

if a >= 0 and b == 2**N.

Peter
Sep 15 '06 #8
Peter,

That was what I was thinking along the lines of, It's been two years
since I finished my CS degree and working in mechanical engineering
means I've nearly forgotten it all! :(

Thanks, I'll write a function in my app to handle this...

Hugh
>(5 + 7) % 10
2

In this context '%' is called 'modulo operator'. What John and Marc
suggested is basically the same operation for the special case of binary
numbers, i. e.

a % b == a & (b-1)

if a >= 0 and b == 2**N.

Peter
Sep 15 '06 #9
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
I've been thinking some more about this and my brain is starting to
work something out...
No need to think too long to come up with the most possibly Q&D solution:

res = int(str(5 + 7)[-1])

Like it ?-)

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Sep 15 '06 #10
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"...

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Sep 15 '06 #11
Bruno Desthuilliers a écrit :
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"...
But you must use numbers smaller than 10 as input! Still :
>>8 ^ 2
10

:D

Sep 15 '06 #12
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
Sep 15 '06 #13
In article <45***********************@news.free.fr>, Bruno Desthuilliers 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
>I've been thinking some more about this and my brain is starting to
work something out...

No need to think too long to come up with the most possibly Q&D solution:

res = int(str(5 + 7)[-1])
Am I missing something subtle in the question or is there some reason
that nobody has posted the correct solution:

(a + b) % 10

?
Sep 15 '06 #14
Christophe wrote:
Bruno Desthuilliers a écrit :
>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"...

But you must use numbers smaller than 10 as input! Still :
>>>8 ^ 2
10
:D
Still fails:
>>8 ^ 7
15

Sorry !-p

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Sep 15 '06 #15
Jon Ribbens wrote:
In article <45***********************@news.free.fr>, Bruno Desthuilliers wrote:

No need to think too long to come up with the most possibly Q&D solution:

res = int(str(5 + 7)[-1])

Am I missing something subtle in the question or is there some reason
that nobody has posted the correct solution:

(a + b) % 10

?
You're not missing anything. That's the obvious solution. We're just
celebrating our non-Dutchness at the moment.
>>def bound(value, maxBound):
.... while value maxBound:
.... value -= maxBound
.... return value
....
>>bound(5 + 6, 10)
1
>>bound(8 + 7, 10)
5

See? I'm definitely not Dutch.

--Jason

Sep 15 '06 #16

Jason wrote:
Jon Ribbens wrote:
In article <45***********************@news.free.fr>, Bruno Desthuilliers wrote:
>
No need to think too long to come up with the most possibly Q&D solution:
>
res = int(str(5 + 7)[-1])
Am I missing something subtle in the question or is there some reason
that nobody has posted the correct solution:

(a + b) % 10

?

You're not missing anything. That's the obvious solution. We're just
celebrating our non-Dutchness at the moment.
>def bound(value, maxBound):
... while value maxBound:
Does non-Dutchness require or permit bugs like instead of >= or are
you merely having a braino?
... value -= maxBound
... return value
...
>bound(5 + 6, 10)
1
>bound(8 + 7, 10)
5

See? I'm definitely not Dutch.

--Jason
Sep 15 '06 #17
Jon Ribbens a écrit :
In article <45***********************@news.free.fr>, Bruno Desthuilliers 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
>>>I've been thinking some more about this and my brain is starting to
work something out...

No need to think too long to come up with the most possibly Q&D solution:

res = int(str(5 + 7)[-1])


Am I missing something subtle in the question or is there some reason
that nobody has posted the correct solution:

(a + b) % 10
I'm afraid Peter Otten did a couple hours before you. But please note
that my solution *is* actually 100% correct (base 10 assumed). It may be
totally inefficiant, but that's another problem !-)
Sep 15 '06 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: Method Man | last post by:
Q: Write a C/C++ program that takes an unsigned integer and adds 1 to it without using the plus or minus signs. Don't worry about overflow issues. It is cheating to use assembly commands, or to...
0
by: Bryan Parkoff | last post by:
I break one U_WORD variable into two U_BYTE variables. I prefer to manipulate two U_BYTE variables instead of one U_WORD variable using Carry. Please look at my example using U_WORD variable...
16
by: archilleswaterland | last post by:
Hi, I am using a compiler that does not support long int (32 bits) so I am using 2 int's to do the work my problem int a; int b; int c;
5
by: campbellbrian2001 | last post by:
I'm trying to get the "Carry data over to new record" code to work from Allen Browne's site: http://allenbrowne.com/ser-24.html I follwed the instruction explicitly and somethings not working......
16
by: codergem | last post by:
How to add two numbers without using the plus operator?
3
chunk1978
by: chunk1978 | last post by:
hi there... i'm trying to make one text field contain the value of another... i'm assuming there's a really simple solution... here's a code i quickly wrote to further explain my issue: ...
13
by: wavbuser | last post by:
Is there a simple way, without setting up a module, to get certain data fields to carry over to a new record as well in MS Access 2003. I've tried alot of modifications to the "default value" but...
0
by: ikilobo | last post by:
I was using GMP library for build an binary arithmetic, but for multiplication, I want to return a result a multiplication without carry.. How can I implementing it? nb: the code below give a...
0
by: Alisha Harris | last post by:
I have a form that also contains a subform. I have tried to input Allen Browne's code to have the last records entered on the form to carry over when a new record is added. However, nothing is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.