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

标签 c integer bit-manipulation 32-bit

我刚开始阅读 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/

相关文章:

c - 使用 INTXX_C 宏和执行类型转换到文字之间有什么区别?

class - dart 编程中整数类中的基数是什么?

javascript - 读取数值中的选定字节

java - Xor 从有效位开始

java - 在字节数组上进行右位旋转/循环移位的最快方法是什么

c - 在C中释放内存不起作用

c - 用 C 处理进程

algorithm - 整数压缩

c - 从 OS X 上 C 中的 dlopen()ed 动态库访问主程序全局结构/指针

c - 如何使用C套接字API连接到我自己的计算机上的服务器?