使用32位cpu的64位总和 [英] sum of 64 bits using 32 bits cpu
问题描述
我在下面的问题中需要帮助。
我有一个知道在32位上进行计算的cpu(unsigned
整数(
写一个函数,得到2个64位数字并返回它们的总和。
我从以下结构开始。
typedef struct {
unsigned int low;
unsigned int high;
} 64bits;
并定义nums
64bits num1.num2;
1.我知道无符号整数的极限是~64000
的情况从0开始溢出它。?
2.有人可以为这个问题添加解决方案吗?
Hi, I need help in the following question .
I have a cpu that knows to do the computations on 32 bits(unsigned
integer(
write a function that gets 2 64 bits numbers and return their sum.
I start with the following structure.
typedef struct{
unsigned int low;
unsigned int high;
}64bits;
and define the nums
64bits num1.num2;
1.I know that the limit of unsigned integer is ~64000 what happend in
case of overflow it start again from 0.?
2.could someone add solution for the question?
推荐答案
5月13日18:17,sarahh< cohen ... @ gmail.comwrote:
On 13 May, 18:17, sarahh <cohen...@gmail.comwrote:
您好,我在以下问题中需要帮助。
我有一个知道在32位上进行计算的cpu(无符号
整数(
写一个得到2 6的函数) 4位数字并返回它们的总和。
我从以下结构开始。
typedef struct {
unsigned int low;
unsigned int high;
} 64位;
并定义nums
64bits num1.num2;
Hi, I need help in the following question .
I have a cpu that knows to do the computations on 32 bits(unsigned
integer(
write a function that gets 2 64 bits numbers and return their sum.
I start with the following structure.
typedef struct{
unsigned int low;
unsigned int high;
}64bits;
and define the nums
64bits num1.num2;
您是否正在使用C89编译器
表示您没有未签名的长期
可用吗?
Are you working on a C89 compiler which
means you don''t have unsigned long long
available ?
1.我知道无符号整数的限制是〜64000发生的事情
溢出的情况它从0开始。?
1.I know that the limit of unsigned integer is ~64000 what happend in
case of overflow it start again from 0.?
UINT_MAX保证至少为65535.它可以更多,实际上标准没有指定
上限。是的,它会在到达时徘徊
UINT_MAX + 1
UINT_MAX is guaranteed to be at least 65535. It could
be a lot more , in fact the standard doesn''t specify an
upper bound. And yes it will wrap around upon reaching
UINT_MAX + 1
2.有人可以为问题添加解决方案吗?
2.could someone add solution for the question?
这是家庭作业吗?
Is it homework ?
sarahh写道:
sarahh writes:
1.我知道无符号整数的极限是~64000
1.I know that the limit of unsigned integer is ~64000
....在主机上,unsigned int是32位的就像你的问题,是的....
....on a host where unsigned int is 32-bit like on your question, yes....
如果发生溢出,它会从0开始再次发生什么?
what happend in case of overflow it start again from 0.?
是的。使用32位无符号int,0xffffffffU(所有位= 1)+ 1U == 0.
无符号算术定义为模数(最大值+ 1)算术。
>
(严格来说,这不是所谓的C语言溢出
- 正是因为它定义明确。溢出是发生在
$ b上的事情$ b签名整数,其结果未定义。)
Yes. With 32-bit unsigned int, 0xffffffffU (all bits = 1) + 1U == 0.
Unsigned arithmetic is defined as modulo-(max value + 1) arithmetic.
(Strictly speaking this is not what is called overflow in the C language
- precisely because it is well-defined. "overflow" is what happens to
signed integers, where the result is not defined.)
2.是否有人为问题添加解决方案?
2.could someone add solution for the question?
家庭作业的重点是边做边学。但是如果你需要一个提示:
想想你如何手工添加两位数字,并认为
的低位和高位是一个数字的两位数。 (在基础0x100000000
而不是基数10,但是嘿......)你需要以某种方式检查
是否有来自添加低的进位数字。
-
Hallvard
The point of homework is to learn by doing. But if you need a hint:
Think of how you do addition of two-digit numbers by hand, and think
of low and high as the two digits of a number. (In base 0x100000000
instead of base 10, but what the hey...) You need to check somehow
whether there was carry from adding the "low" digits.
--
Hallvard
在13×?×?×?,20 :45,Hallvard B Furuseth< hbfurus ... @ usit.uio.no>
写道:
On 13 ×?×?×?, 20:45, Hallvard B Furuseth <h.b.furus...@usit.uio.no>
wrote:
sarahh写道:
sarahh writes:
1.我知道主机上无符号整数的限制为~64000
1.I know that the limit of unsigned integer is ~64000
...其中unsigned int在你的问题上是32位的,是的....
...on a host where unsigned int is 32-bit like on your question, yes....
如果发生溢出,它会从0开始再次发生什么?
what happend in case of overflow it start again from 0.?
是的。使用32位无符号int,0xffffffffU(所有位= 1)+ 1U == 0 ..
无符号算术定义为模数(最大值+ 1)算术。
(严格来说,这不是所谓的C语言溢出
- 正是因为它定义明确。溢出是发生在
签名的整数,其结果未定义。)
Yes. With 32-bit unsigned int, 0xffffffffU (all bits = 1) + 1U == 0..
Unsigned arithmetic is defined as modulo-(max value + 1) arithmetic.
(Strictly speaking this is not what is called overflow in the C language
- precisely because it is well-defined. "overflow" is what happens to
signed integers, where the result is not defined.)
2.是否可以为此问题添加解决方案?
2.could someone add solution for the question?
家庭作业的重点是边做边学。但是如果你需要一个提示:
想想你如何手工添加两位数字,并认为
的低位和高位是一个数字的两位数。 (在基础0x100000000
而不是基数10,但是嘿......)你需要以某种方式检查
是否有来自添加低的进位数字。
-
Hallvard
The point of homework is to learn by doing. But if you need a hint:
Think of how you do addition of two-digit numbers by hand, and think
of low and high as the two digits of a number. (In base 0x100000000
instead of base 10, but what the hey...) You need to check somehow
whether there was carry from adding the "low" digits.
--
Hallvard
谢谢,知道我更了解它怎么样没有签名,如果我加上max + 1,会发生什么?b / b?
Thanks,know I understand it better what about not unsigned, what
happend if I add to max+1 ?
这篇关于使用32位cpu的64位总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!