gpt4 book ai didi

c - 关于无符号整数下溢的 C 行为问题

转载 作者:太空狗 更新时间:2023-10-29 16:29:41 25 4
gpt4 key购买 nike

我在很多地方都读到,无符号整数溢出在 C 中定义明确,与有符号整数溢出不同。

underflow一样吗?

例如:

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

谢谢。

我不记得在哪里,但我在某处读到无符号整数类型的算术是模块化的,所以如果是这种情况,那么 -1 == UINT_MAX mod (UINT_MAX+1)。

最佳答案

§6.2.5,第 9 段:

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.

编辑:

抱歉,引用错误,但结果仍然确定。正确的引用是§6.3.1.3(有符号和无符号整数转换):

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 .

关于c - 关于无符号整数下溢的 C 行为问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2760502/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com