473,785 Members | 2,842 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5178
In <11************ **********@i42g 2000cwa.googleg roups.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
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 #10

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

Similar topics

25
2237
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 write a preprocessor that uses ASCII codes to insert the plus or minus sign. There are many solutions, some more elegant than others.
0
1415
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 below. typedef unsigned char U_BYTE; typedef unsigned short int U_WORD; U_WORD HighLow = 0x00FE; HighLow += 0x04; U_BYTE Carry = HighLow >> 8;
16
4062
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
3949
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... any clues? Thanks all!! Brian
16
12793
by: codergem | last post by:
How to add two numbers without using the plus operator?
3
2179
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: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />...
13
11632
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 none of them seem to work. If anyone can help me out it would be greatly appreciated.
0
1721
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 result of multiplication with carry void multi(){ int m_bit;int m_bit2; char *op1; char *op2; char *final;
0
1133
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 carrying over. Here is my code. Option Compare Database Public Function CarryOver(frm As Form, strErrMsg As String, ParamArray avarExceptionList()) As Long On Error GoTo Err_Handler 'Purpose: Carry over the same fields to a new record, based...
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10330
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10153
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10093
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8976
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3654
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.