gpt4 book ai didi

c - 为什么 abs(0x80000000) == 0x80000000?

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

我刚开始阅读 Hacker's Delight并将 abs(-231) 定义为 -231。这是为什么?

我在几个不同的系统上尝试了 printf("%x", abs(0x80000000)),我在所有这些系统上都返回了 0x80000000。

最佳答案

实际上,在 C 中,行为是未定义的。来自 C99 标准,§7.20.6.1/2:

The abs, labs, and llabs functions compute the absolute value of an integer j. If the result cannot be represented, the behavior is undefined.

及其脚注:

The absolute value of the most negative number cannot be represented in two’s complement.

关于c - 为什么 abs(0x80000000) == 0x80000000?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2539178/

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