c++ - C++11 标准是否保证零值有符号整数的一元减为零?

标签 c++ c++11 integer arithmetic-expressions unary-operator

C++11 标准是否保证零值的一元减有符号整数 是零吗?

例如:

int zero = 0;
int n = -zero;
int m = -0;
assert(memcmp(&n, &zero, sizeof(int)) == 0);
assert(memcmp(&m, &zero, sizeof(int)) == 0);

我知道-00在二进制的恭维表示中是相同的,但我想知道标准是否允许有符号整数零的否定为其他表示的负零,例如一个人的恭维或有符号幅度。

我在 C++11 草案中所能找到的只是 §5.3.1,第 8 段:

The operand of the unary-operator shall have arithmetic or unscoped enumeration type and the result is the negation of its operand. Integral promotion is performed on integral or enumeration operands. The negative of an unsigned quantity is computed by subtracting its value from 2^n, where n is the number of bits in the promoted operand. The type of the result is the type of the promoted operand.



我在草案中找不到否定的定义。

动机 :我正在为一个库(最终可能是开源的)编写一个专门的整数解析器,我想知道我是否应该关注 "-0" 的可能性。在不常见的架构上被解释为负零有符号整数。

备注 : 我已经知道负零浮点数了。

最佳答案

该标准不要求整数的位模式,因为它旨在适用于尽可能广泛的机器。 C++ 编译器完全有可能使用补码,其中零和负零是不同的。

关于c++ - C++11 标准是否保证零值有符号整数的一元减为零?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59743380/

相关文章:

c++ - 比较运算符 == 不起作用,我该如何让它起作用? [cpp]

c++ - 在 C 和 C++ 中动态声明数组大小

c++ - 可变参数模板。一些问题

c++ - 使用 sstream 验证失败

integer - ocaml 查看 int 的特定数字

c++ - 如何在 istream& 运算符中输入字符串

c++ - ifstream 的 std::getline,使用参数字符串或 char *

c++ - 使用可变参数模板将 vector 中的参数应用于函数时出现奇怪错误

C++ 日志记录类实例标识符

c# - 如何将有符号整数转换为无符号整数?