473,796 Members | 2,444 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

integer addition taking CARRY into account

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[2];
int b[2];
int c[2];

I want to

c[0]=a[0]+b[0]; -----> Step one
c[1]=a[1]+b[1]+carry from Step one

how do I get the carry from step one in C programming ?
Any hints ?

thanks for your time,
archilles

Nov 14 '05
16 4065
CBFalconer wrote:
Peter Nilsson wrote:
CBFalconer wrote:
Peter Nilsson wrote:
Ben Pfaff wrote:
> <ar************ ****@hotmail.co m> writes:
>> 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[2];
>> int b[2];
>> int c[2];
>>
>> I want to
>>
>> c[0]=a[0]+b[0]; -----> Step one
>> c[1]=a[1]+b[1]+carry from Step one
>
> If unsigned integers are acceptable (replace `int' by `unsigned'
> above), then I believe that the following will work:
> c[0] = a[0] + b[0];
> c[1] = a[1] + b[1] + (c[0] < a[0] || c[0] < b[0]);

Or...

c[0] = a[0] + b[0];
c[1] = a[1] + b[1] + (a[0] > -1u - b[0]);

To be able to extend things limit the range to 0..INT_MAX in each
component, and then:


The sign bit is problematic. If you're going to use non-negative
int values only, then two ints will only yield 30 bits. Two
unsigned ints guarantees 32-bits.


There is no sign bit in an unsigned int.


And? The OP wanted a 32-bit range. Your limitation means the
method only uses 30 bits on a 16-bit [unsigned] int machine.
Just think of converting those unsigned values into registers
with one less bit and one overflow bit. That bit is the carry.
c[0] = a[0] + b[0];


Potential undefined behaviour on int overflow!


I said using unsigned above. Thus no int overflows


Fair enough, but...
c[1] = a[1] + b[1] + !!(c[0] & ~INT_MAX);


Obviously the OP is not using a conforming implementation, but
the question is answerable in C terms because it can be useful
to write big (or bigger) num libraries in the manner being
described here.

Keeping to ISO C topicallity, ~INT_MAX may be a trap
representation.


Not in an unsigned int.


INT_MAX has type int. The complement (~) will be performed using
signed int arithmetic.

Before you suggest ~(unsigned)INT_ MAX, realise that UINT_MAX==INT_M AX
is allowed, in which case your bit mask is 0.

You could replace INT_MAX with (-1u/2), but as I said, that may mean
you don't meet the 32-bit requirement.

--
Peter

Nov 14 '05 #11
Peter Nilsson wrote:
.... snip ...
INT_MAX has type int. The complement (~) will be performed using
signed int arithmetic.

Before you suggest ~(unsigned)INT_ MAX, realise that UINT_MAX==INT_M AX
is allowed, in which case your bit mask is 0.

You could replace INT_MAX with (-1u/2), but as I said, that may mean
you don't meet the 32-bit requirement.


I don't accept this. All int bits have a weight, per standard. An
unsigned int includes the sign bit, which must have a new weight.
I don't believe UINT_MAX==INT_M AX can be consistent with the
standard.

--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #12
CBFalconer <cb********@yah oo.com> wrote:
Peter Nilsson wrote:
INT_MAX has type int. The complement (~) will be performed using
signed int arithmetic.

Before you suggest ~(unsigned)INT_ MAX, realise that UINT_MAX==INT_M AX
is allowed, in which case your bit mask is 0.

You could replace INT_MAX with (-1u/2), but as I said, that may mean
you don't meet the 32-bit requirement.


I don't accept this. All int bits have a weight, per standard. An
unsigned int includes the sign bit, which must have a new weight.


I can't find anything which requires that there is one more value bit in
unsigned types than in their corresponding signed type. An unsigned int
could simply have one more padding bit than a signed int.

Richard
Nov 14 '05 #13
CBFalconer wrote:
Peter Nilsson wrote:

... snip ...

INT_MAX has type int. The complement (~) will be performed using
signed int arithmetic.

Before you suggest ~(unsigned)INT_ MAX, realise that UINT_MAX==INT_M AX is allowed, in which case your bit mask is 0.

You could replace INT_MAX with (-1u/2), but as I said, that may mean you don't meet the 32-bit requirement.


I don't accept this. All int bits have a weight, per standard. An
unsigned int includes the sign bit, which must have a new weight.
I don't believe UINT_MAX==INT_M AX can be consistent with the
standard.


The standard is quite clear. Indeed, this has been pointed out to
you before...

6.2.6.2p1: "For unsigned integer types other than unsigned char,
the bits of the object representation shall be divided into two
groups: value bits and padding bits ..."

6.2.6.2p2: "For signed integer types, the bits of the object
representation shall be divided into three groups: value bits,
padding bits, and the sign bit. ... (if there are M value bits
in the signed type and N in the unsigned type, then M <= N).
...."

Thus, a sign bit is not a value bit, and the sign bit of a
signed integer need not contribute a value in the corresponding
unsigned type. [The only exception is unsigned char (6.2.6.1p3)]

--
Peter

Nov 14 '05 #14
Peter Nilsson wrote:
CBFalconer wrote:
.... snip ...

I don't accept this. All int bits have a weight, per standard. An
unsigned int includes the sign bit, which must have a new weight.
I don't believe UINT_MAX==INT_M AX can be consistent with the
standard.


The standard is quite clear. Indeed, this has been pointed out to
you before...

6.2.6.2p1: "For unsigned integer types other than unsigned char,

.... snip ...
Thus, a sign bit is not a value bit, and the sign bit of a
signed integer need not contribute a value in the corresponding
unsigned type. [The only exception is unsigned char (6.2.6.1p3)]


I guess I have to concede. However I doubt that any such perverse
systems exist or have existed, apart from the DS9000. Because it
seems so unreasonable to me I will surely forget all about it again
:-[

--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #15
On Wed, 06 Apr 2005 04:12:44 +0000, CBFalconer wrote:
Peter Nilsson wrote:
CBFalconer wrote: Thus, a sign bit is not a value bit, and the sign bit of a
signed integer need not contribute a value in the corresponding
unsigned type. [The only exception is unsigned char (6.2.6.1p3)]


I guess I have to concede. However I doubt that any such perverse
systems exist or have existed, apart from the DS9000. Because it
seems so unreasonable to me I will surely forget all about it again
:-[


With most hardware gravitating to 2's complement signed integer
representations these days you're probably right. But let's say you had a
1's complement or sign-magnitude based architecture that supported
integer arithmetic on just that representation, i.e. no "unsigned
compatible" arithmetic. A way to simplify the implementation of unsigned
types might be to use the signed representation but require the sign bit
to be zero. In effect it becomes a padding bit and setting it to 1 could
be considered a trap representation. It helps if the imlementation has
unusual or large register sizes because e.g. a 16 bit unsigned int has to
use all bits as value bits. And it has to do that for unsigned char
irresepctive of its size.

So I wouldn't call it unreasonable, but it is unlikely these days.

Lawrence
Nov 14 '05 #16


"ar************ ****@hotmail.co m" wrote:
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[2];
int b[2];
int c[2];

I want to

c[0]=a[0]+b[0]; -----> Step one
c[1]=a[1]+b[1]+carry from Step one

how do I get the carry from step one in C programming ?
Any hints ?

thanks for your time,
archilles


I am assuming it is an 8bit CPU. Best case you be a call to an ASM
subroutine.

What if?

you used 4 unsigned bytes but used Integer math

unsigned char A[4];
unsigned char B[4];
unsigned char C[4];
unsigned int Temp;

Temp = A[0] + B[0];
C[0] = Temp & 0xFF;

Temp = A[1] + B[1] + (Temp>>8);
C[1] = Temp & 0xFF;

and so on... with proper casts (of course) ect..

Nov 14 '05 #17

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

Similar topics

3
2613
by: Pierre Espenan | last post by:
A have a long integer class. The built integer type within a conditional statement returns bool false for int i=0 and bool true for any other non zero value. I want my long integer class to have similar behavior. My class looks like this: #ifndef long_int_H #define long_int_H #include <string> using namespace std; typedef valarray<complex<double> > VCD;
2
1629
by: akickdoe22 | last post by:
i could really use help finishing this addition program. I'm stuck on the part that allows you to add any two large integers,up to 100 digits,(pos+pos, neg+neg, and pos+neg). could use hints ideas code anything for neg+pos addition. CODE SO FAR: class INT { int digits; char sign; public:
24
2721
by: Alex Vinokur | last post by:
Consider the following statement: n+i, where i = 1 or 0. Is there more fast method for computing n+i than direct computing that sum? -- Alex Vinokur email: alex DOT vinokur AT gmail DOT com http://mathforum.org/library/view/10978.html
20
9180
by: GS | last post by:
The stdint.h header definition mentions five integer categories, 1) exact width, eg., int32_t 2) at least as wide as, eg., int_least32_t 3) as fast as possible but at least as wide as, eg., int_fast32_t 4) integer capable of holding a pointer, intptr_t 5) widest integer in the implementation, intmax_t Is there a valid motivation for having both int_least and int_fast?
19
2076
by: junky_fellow | last post by:
How the unsigned to signed integet conversion is done ? For eg: unsigned int ui = 100; int si = ui; What will be the value if "si" is printed ? How this conversion is done ? Thanx for any help in advance ....
2
8121
by: confusedKaran | last post by:
Hi, I am currently making a program which can add and multiply two numbers with infinite amount of digits. The addition part of it I did by taking the input as a string and then one by one addiing the digits(with a carry) and putting the number in another string, till or both of them finishes. the multiplication I dont know how to start it. Suggestions or source code will be appreciated Regards Karan
31
3150
by: Pesso | last post by:
What happens if you multiple two integers and the result overflows the MAX_INT in C? Is there a way to trap the condition when it happens?
6
5001
by: Chris Becke | last post by:
I *know* my CPU has opcodes that can do this - when adding (or subtracting) there is a carry flag that can be invoked to make the result essentially 1 bit longer than the data size used in calculations. When multiplying two numbers, the CPU automatically returns a double width result. c & c++ give programmers these bloody ridiculous integer types of undetermined size and no really optimal way to even build a variable width number library...
2
2166
by: susheela s | last post by:
i wrote a program to add bytes in array in such a way that when i add zeroth byte of two array sum should retained and carry must be added to next addition of bytes(ie array index 1 bytes) this is program which i wrote using class #include<iostream.h> #define N 2 typedef unsigned char byte; class Megaint { private:
0
9680
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
10455
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
10228
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
10173
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
10006
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9052
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...
1
7547
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3731
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.