无符号短加法/减法溢出 [英] unsigned short addition/subtraction overflow
问题描述
C中有1到4个定义的行为吗?
unsigned short i;
unsigned long li; / * 32位宽* /
1. i = 65535 + 3;
2. i = 1 - 3;
3. li =(无符号长)0xFFFFFFFF + 3;
4. li = 1 - 3;
TIA
Andy
Hi,
Are 1 through 4 defined behaviors in C?
unsigned short i;
unsigned long li; /* 32-bit wide */
1. i = 65535 + 3;
2. i = 1 - 3;
3. li = (unsigned long)0xFFFFFFFF + 3;
4. li = 1 - 3;
TIA
Andy
推荐答案
2003-12-21,Andy< bi ***** @ hotmail.com>写道:
On 2003-12-21, Andy <bi*****@hotmail.com> wrote:
C中有1到4个定义的行为吗?
unsigned short i;
unsigned long li; / * 32位宽* /
1. i = 65535 + 3;
2. i = 1 - 3;
3. li =(无符号长)0xFFFFFFFF + 3;
4. li = 1 - 3;
Are 1 through 4 defined behaviors in C?
unsigned short i;
unsigned long li; /* 32-bit wide */
1. i = 65535 + 3;
2. i = 1 - 3;
3. li = (unsigned long)0xFFFFFFFF + 3;
4. li = 1 - 3;
是的。
- 詹姆斯>
James Hu写道:
James Hu wrote:
2003-12-21,Andy< bi ***** @ hotmail.com>写道:
On 2003-12-21, Andy <bi*****@hotmail.com> wrote:
C中有1到4个定义的行为吗?
unsigned short i;
unsigned long li; / * 32位宽* /
1. i = 65535 + 3;
2. i = 1 - 3;
3. li =(无符号长)0xFFFFFFFF + 3;
4. li = 1 - 3;
Are 1 through 4 defined behaviors in C?
unsigned short i;
unsigned long li; /* 32-bit wide */
1. i = 65535 + 3;
2. i = 1 - 3;
3. li = (unsigned long)0xFFFFFFFF + 3;
4. li = 1 - 3;
是的。
Yes.
No.
65536是INT_MAX的允许值。
(65535 + 3)将是整数溢出
和那种情况下的未定义行为。
-
pete
No.
65536 is an allowable value for INT_MAX.
(65535 + 3) would be integer overflow
and undefined behavior in that case.
--
pete
" pete" < PF ***** @ mindspring.com> schrieb im Newsbeitrag
新闻:3F *********** @ mindspring.com ...
"pete" <pf*****@mindspring.com> schrieb im Newsbeitrag
news:3F***********@mindspring.com...
James Hu写道:
James Hu wrote:
2003-12-21,Andy< bi ***** @ hotmail.com>写道:
On 2003-12-21, Andy <bi*****@hotmail.com> wrote:
C中有1到4个定义的行为吗?
unsigned short i;
unsigned long li; / * 32位宽* /
1. i = 65535 + 3;
2. i = 1 - 3;
3. li =(无符号长)0xFFFFFFFF + 3;
4. li = 1 - 3;
Are 1 through 4 defined behaviors in C?
unsigned short i;
unsigned long li; /* 32-bit wide */
1. i = 65535 + 3;
2. i = 1 - 3;
3. li = (unsigned long)0xFFFFFFFF + 3;
4. li = 1 - 3;
是的。
Yes.
没有。
65536是允许的INT_MAX的值。
(65535 + 3)将是整数溢出
和那种情况下的未定义行为。
No.
65536 is an allowable value for INT_MAX.
(65535 + 3) would be integer overflow
and undefined behavior in that case.
来自N869:6.2.5
9有符号整数类型的非负值范围是
的子范围
对应的无符号整数类型,以及每个
类型的
值是相同的.28)涉及无符号操作数的计算永远不会
溢出,
因为无法用结果无符号
整数类型表示的结果是
减少模数比一个大于
$的最大值的数字b $ b可以是由结果类型代表的
。
然而,我不确定你是谁t $ / $
i = 1 - 3;
和
li = 1 - 3;
但我认为是据我所知定义整数促销
规则。
欢呼
罗伯特
From N869: 6.2.5
9 The range of nonnegative values of a signed integer type is a subrange of
the
corresponding unsigned integer type, and the representation of the same
value in each
type is the same.28) A computation involving unsigned operands can never
overflow,
because a result that cannot be represented by the resulting unsigned
integer type is
reduced modulo the number that is one greater than the largest value that
can be
represented by the resulting type.
However, I am not sure about
i = 1 - 3;
and
li = 1 - 3;
but I think it is defined as far as I understand the integer promotion
rules.
cheers
Robert
这篇关于无符号短加法/减法溢出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!