关于无符号整数下溢Ç行为问题 [英] Question about C behaviour for unsigned integer underflow

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

问题描述

我在该整数用C溢出明确不像签署对口许多地方阅读。

I have read in many places that integer overflow is well-defined in C unlike the signed counterpart.

下溢一样吗?

例如:

unsigned int x = -1; // Does x == UINT_MAX?

感谢。

我不记得在哪里,但我在其他地方见过无符号整型算术是模块化的,所以如果说当时的情况下-1 == UINT_MAX MOD(UINT_MAX + 1)。

I can't recall where, but i read somewhere that arithmetic on unsigned integral types is modular, so if that were the case then -1 == UINT_MAX mod (UINT_MAX+1).

推荐答案

<击>§6.2.5,第9段:

§6.2.5, paragraph 9:

在无符号的一种计算
  操作数可以从不溢出,因为一个
  结果,不能再由psented $ P $
  所产生的无符号整数类型
  降低模是第一位的
  大于最大值,该值
  可通过由此而来psented重新$ P $
  类型。

编辑:

对不起,错了参考,但结果仍是牵制。正确的参考§6.3.1.3(符号和无符号整数转换):

Sorry, wrong reference, but the result is still pinned down. The correct reference is §6.3.1.3 (signed and unsigned integer conversion):

,如果新的类型是
  无符号,值被转换
  反复增加或减去1
  以上的最大值,可以
  重新$ P $的新型psented直到
  该值是在新的范围
  类型。

if the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type.

所以,是的, X == UINT_MAX 。

这篇关于关于无符号整数下溢Ç行为问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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