在C中添加大数字 [英] Adding large numbers in C

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

问题描述

如何在C中添加一个n位数?


问候,

斯里兰卡

How do you add an n-bit number in C?

Regards,
Sri

推荐答案

Sri写道:
如何在C中添加一个n位数?

问候,
Sri
How do you add an n-bit number in C?

Regards,
Sri



要么使用第三方库(GIYF),要么使用

无符号长整数(或C99中的无符号长long或编译器)

提供它作为扩展)并自己处理携带位。


过去,它不是C问题,而是更通用的编程问题。


HTH,

- g


-

Artie Gold - 德克萨斯州奥斯汀
http://goldsays.blogspot.com

你不能亲吻*除非你错过**

[* - 保持简单,愚蠢。 ** - 简单,愚蠢。]


Either by using a third party library (GIYF) or by using an array of
unsigned longs (or unsigned long longs in C99 or if your compiler
supplies it as an extension) and dealing with carry bits yourself.

Past that, it''s not a C question, but a more general programming question.

HTH,
--ag

--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com
"You can''t KISS* unless you MISS**"
[*-Keep it simple, stupid. **-Make it simple, stupid.]


文章< 11 ******************** **@v46g2000cwv.googlegroups .com> ;,

Sri< sr ******* @ gmail.com>写道:

:你如何在C中添加一个n位数?


我?我会找到一个已经实现它的库。


更一般地说,你从无符号长开始,弄清楚有多少

位在那里,将你的数字分成未签名的

多头的序列。然后从低端无符号长点开始,弄清楚如果加上两个会溢出的
。如果它不会溢出,添加两个并存储

结果和携带0。如果它会溢出,计算

的结果将是mod / 2
块中的位数的mod 2,存储它,并且携带1 ;。继续左边的下一个unsigned long

对,这次只考虑进位。重复。


提示:仔细阅读C如何添加无符号长整数。

可能是你可以使用的捷径。

-

没有人有权通过以下方式摧毁他人的信仰

要求经验证据。 - Ann Landers
In article <11**********************@v46g2000cwv.googlegroups .com>,
Sri <sr*******@gmail.com> wrote:
:How do you add an n-bit number in C?

Me? I''d find a library that already implemented it.

More generally, you start with an unsigned long, figure out how many
bits are in that, chunk your numbers into sequences of the unsigned
longs. Then start from the low end unsigned longs, figure out if adding
the two would overflow. If it would not overflow, add the two and store
the result and "carry a 0". If it would overflow, calculate what the
result of the addition would be mod 2 to the number of bits in the
chunk, store that, and "carry a 1". Go on to the next unsigned long
pair to the left, only this time take the carry into account. Repeat.

Hint: read carefully how C does addition of unsigned longs. There
might be shortcuts you can use.
--
"No one has the right to destroy another person''s belief by
demanding empirical evidence." -- Ann Landers


Hello Walter,


我的上一篇文章中没有以下逻辑:


"它会溢出,计算添加的结果是什么?
mod 2到块中的位数


另外如何检查操作的结果是否会溢出.......我们不能将结果保存在unsigned long变量中

并检查它的值,因为它本身已被包裹。有没有

的方式知道没有实际添加?


Rajesh

Hello Walter,

I didn''t got the following logic from your last post :

"it would overflow, calculate what the result of the addition would be
mod 2 to the number of bits in the chunk"

Also how to check whether the result of an operation would
overflow.......we can''t keep the result in an unsigned long variable
and check it''s value as it would have been wrapped around. Is there any
way to know that without actual addition?

Rajesh


这篇关于在C中添加大数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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