整数加法考虑CARRY [英] integer addition taking CARRY into account

查看:62
本文介绍了整数加法考虑CARRY的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我使用的编译器不支持long int(32位)

所以我使用2 int'来做工作


我的问题


int a [2];

int b [2];

int c [2];


我想


c [0] = a [0] + b [0]; ----->第一步

c [1] = a [1] + b [1] +从第一步开始


如何从第一步获得进位C编程?

任何提示?


感谢您的时间,

archilles

解决方案

" ar **************** @ hotmail.com" < AR **************** @ hotmail.com>写道:

我使用的编译器不支持long int(32位)
所以我使用2 int'来做这项工作

我的问题

int a [2];
int b [2];
int c [2];

我想要

c [0] = a [0] + b [0]; ----->第一步
c [1] = a [1] + b [1] +从第一步开始




如果无符号整数可以接受(替换`int ''通过'unsigned''

以上),然后我相信以下内容将起作用:

c [0] = a [0] + b [0];

c [1] = a [1] + b [1] +(c [0]< a [0] || c [0]< b [0]);

如果错误,我肯定有人会纠正我。

-

int main(void){char p [] =" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz。 \

); while(* q){i + = strchr(p,* q ++) - p; if(i> =(int)sizeof p)i- = sizeof p-1; putchar(p [i] \

);}返回0;}


Ben Pfaff写道:

" ar ***** ***********@hotmail.com" < ar **************** @ hotmail.com>



写道:

我使用的编译器不支持long int(32位)
所以我使用2 int'来做工作

我的问题

int a [2];
int b [2];
int c [2];

我想

c [0] = A [0] + b [0]; ----->第一步
c [1] = a [1] + b [1] +从第一步开始


如果无符号整数是可接受的(用`替换`int''上面没有签名''
,我认为以下内容可行:
c [0] = a [0] + b [0];
c [1] = a [1 ] + b [1] +(c [0]< a [0] || c [0]< b [0]);




或。 ..


c [0] = a [0] + b [0];

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


-

彼得


在文章< 87 ************ @ benpfaff.org>,

Ben Pfaff< bl*@cs.stanford。 EDU>写道:

如果无符号整数是可以接受的(将'int''替换为'unsigned''
,那么我相信以下内容将会起作用:
c [0] = a [0] + b [0];
c [1] = a [1] + b [1] +(c [0]< a [0] || c [0 ]< b [0]);




携带的表达式不必要地复杂化。如果在第一次添加时出现溢出

,则c [0]将小于* * a [0]和

b [0],因此您可以将其与其中一个:


c [1] = a [1] + b [1] +(c [0]< a [0]);


- Richard


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

解决方案

"ar****************@hotmail.com" <ar****************@hotmail.com> 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[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}


Ben Pfaff wrote:

"ar****************@hotmail.com" <ar****************@hotmail.com>


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.stanford.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


这篇关于整数加法考虑CARRY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆