为什么无符号整数溢出定义的行为,但是有符号整数溢出是不是? [英] Why is unsigned integer overflow defined behavior but signed integer overflow isn't?

查看:777
本文介绍了为什么无符号整数溢出定义的行为,但是有符号整数溢出是不是?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无符号整数溢出是广受C和C ++标准中定义。例如, C99标准§ 6.2.5 / 9 )的状态

Unsigned integer overflow is well defined by both the C and C++ standards. For example, the C99 standard (§6.2.5/9) states

一个计算涉及无符号运算绝不能溢流,
  因为不能再通过将得到的无符号整型psented $ P $结果为是
  减小模数比,可以是在一个最大值大的数目
  再由结果类型psented $ P $。

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.

但是,这两个标准状态符号整数溢出是未定义的行为。再次,从C99标准(§3.4.3/ 1

However, both standards state that signed integer overflow is undefined behavior. Again, from the C99 standard (§3.4.3/1)

的理解过程网络斯内德行为的一个例子是溢流

An example of undefined behavior is the behavior on integer overflow

是否有一个历史或(甚至更好!)由于技术原因这种差异?

Is there an historical or (even better!) a technical reason for this discrepancy?

推荐答案

历史的原因是,大多数C实现(编译器)刚刚使用的任何溢出的行为是最容易重新presentation整数它用来实现。 C实现通常使用的CPU使用的相同重新presentation - 所以溢出的行为,随后再从presentation由CPU所使用的整数

The historical reason is that most C implementations (compilers) just used whatever overflow behaviour was easiest to implement with the integer representation it used. C implementations usually used the same representation used by the CPU - so the overflow behavior followed from the integer representation used by the CPU.

在实践中,它仅适用于可根据本实施不同符号值的重新presentations:一个人的补体,二的补,申请幅度。对于无符号类型没有理由为标准,允许偏差,因为只有一个明显的二元再presentation(标准只允许二进制重新presentation)。

In practice, it is only the representations for signed values that may differ according to the implementation: one's complement, two's complement, sign-magnitude. For an unsigned type there is no reason for the standard to allow variation because there is only one obvious binary representation (the standard only allows binary representation).

相关报价:

C99 6.2.6.1:3

存储在无符号位字段和unsigned char型对象中的值应重新使用纯二进制表示法presented。

Values stored in unsigned bit-fields and objects of type unsigned char shall be represented using a pure binary notation.

C99 6.2.6.2:2

如果符号位为1,该值应以下列方式之一修改:

If the sign bit is one, the value shall be modified in one of the following ways:

- 符号位0被否定(符号和幅度)对应的值;

— the corresponding value with sign bit 0 is negated (sign and magnitude);

- 符号位的值是 - (2N)(补);

— the sign bit has the value −(2N ) (two’s complement);

- 符号位的值是 - (2N - 1)(一补数)

— the sign bit has the value −(2N − 1) (ones’ complement).

目前,所有的处理器使用补重presentation,但符号算术溢出仍然不确定和编译器制造商希望,因为他们使用此undefinedness以帮助优化它仍然不确定。例如,见这博客文章通过伊恩·泰勒兰斯或本的投诉由瓦格纳雾和答案给他的bug报告。


Nowadays, all processors use two's complement representation, but signed arithmetic overflow remains undefined and compiler makers want it to remain undefined because they use this undefinedness to help with optimization. See for instance this blog post by Ian Lance Taylor or this complaint by Agner Fog, and the answers to his bug report.

这篇关于为什么无符号整数溢出定义的行为,但是有符号整数溢出是不是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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