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 16 4063
"ar************ ****@hotmail.co m" <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]);
I am sure someone will correct me if it is wrong.
--
int main(void){char p[]="ABCDEFGHIJKLM NOPQRSTUVWXYZab cdefghijklmnopq rstuvwxyz.\
\n",*q="kl BIcNBFr.NKEzjwC IxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+= strchr(p,*q++)-p;if(i>=(int)si zeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Ben Pfaff wrote: "ar************ ****@hotmail.co m" <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]);
--
Peter
In article <87************ @benpfaff.org>,
Ben Pfaff <bl*@cs.stanfor d.edu> wrote: 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]);
The expression for carry is unnecessarily complicated. If overflow
occurs in the first addition, c[0] will be less than *both* a[0] and
b[0], so you can compare it with either one:
c[1] = a[1] + b[1] + (c[0] < a[0]);
-- Richard
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:
c[0] = a[0] + b[0];
c[1] = a[1] + b[1] + !!(c[0] & ~INT_MAX);
....
c[n] = a[n] + b[n] + !!(c[n-1] & ~INT_MAX);
Just ensure than any use of the values always masks it off with
INT_MAX. To ensure this the c[n] line should maybe become (for all
n, for n >= 1):
c[n] = a[n] + b[n] + !!(c[n-1] & ~INT_MAX);
c[n-1] = c[n-1] & INT_MAX;
The basic problem is that, while the CPU probably has a carry bit,
it is never accessible in standard C. So we have to build one
somewhere.
--
"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
Ben Pfaff wrote: "ar************ ****@hotmail.co m" <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]); I am sure someone will correct me if it is wrong.
Good bet. if a[0] + b[0] overflows, c[0] will be less than either of
them. Only one check is necessary.
If you can get me a job out there, I'll help you with your homework. :-)
--
Joe Wright mailto:jo****** **@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Ben Pfaff wrote: "ar************ ****@hotmail.co m" <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]); I am sure someone will correct me if it is wrong.
Looks right to me, but I think the second line can be
simplified to
c[1] = a[1] + b[1] + (c[0] < a[0]);
That is, the two operands of the `||' will always have
the same value, so only one of them needs be tested.
--
Eric Sosman es*****@acm-dot-org.invalid
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.
c[0] = a[0] + b[0];
Potential undefined behaviour on int overflow!
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.
Coming back to unsigned types, another approach is to split
the unsigned type into two sections...
#define S 8
#define M ((1u << S) - 1)
acc = (a[0] & M) + (b[0] & M);
c[0] = acc & M;
acc = (acc >> S) + (a[0] >> S) + (b[0] >> S);
c[0] |= (acc << S);
acc /= ((-1u >> S) + 1);
c[1] = a[1] + b[1] + acc;
--
Peter
Joe Wright <jo********@com cast.net> writes: If you can get me a job out there, I'll help you with your homework. :-)
Homework? I'm a Ph.D. student. At this point in my academic
career, I assign *other* people the homework...
--
Ben Pfaff
email: bl*@cs.stanford .edu
web: http://benpfaff.org
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. 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 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. Coming back to unsigned types, another approach is to split the unsigned type into two sections...
Which is what I did, except I only used one bit to hold the carry.
--
"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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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;
|
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:
|
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
|
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?
|
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 ....
| |
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
|
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?
|
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...
|
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:
|
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,...
|
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...
| |
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...
|
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...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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
| |