C:无符号运算元减运算符的行为 [英] C: unary minus operator behavior with unsigned operands

查看:184
本文介绍了C:无符号运算元减运算符的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法找到C标准的相关部分完全确定无符号运算一元减运算符的行为。

I can't seem to find the relevant parts in the C standard fully defining the behavior of the unary minus operator with unsigned operands.

2003年的C ++标准(是的,C ++,包涵了几行)说,在5.3.1c7:无符号数的负是由2减去其值^ n,其中计算n是在促进操作数的比特数。

The 2003 C++ standard (yes, C++, bear with me for a few lines) says in 5.3.1c7: 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.

1999年的C标准,但不包括这样一个明确的说法,并没有明确界定的一元 - 无论是在6.5.3.3c1,3也不6.5c4行为。在后者它说一些运营商(单目运算符〜和二元运算符<<,>>中&放大器;,^和|,...)...返回值依赖于整数的内部重新presentations,并有实现定义和不确定的方面有符号的类型。),其中不包括一元减,事情似乎仍然模糊不清。

The 1999 C standard, however, doesn't include such an explicit statement and does not clearly define the unary - behavior neither in 6.5.3.3c1,3 nor in 6.5c4. In the latter it says Some operators (the unary operator ~, and the binary operators <<, >>, &, ^, and |, ...) ... return values that depend on the internal representations of integers, and have implementation-defined and undefined aspects for signed types.), which excludes the unary minus and things seem to remain vague.

这较早前的问题指K&放大器; R ANSI C的书,部分A.7.4.5上面写着无符号数的负是从推动型的最大价值减去的推广价值和增加计算的一个

This earlier question refers to the K&R ANSI C book, section A.7.4.5 that says The negative of an unsigned quantity is computed by subtracting the promoted value from the largest value of the promoted type and adding one.

什么是1999年的C标准相当于从书上面的报价?

What would be the 1999 C standard equivalent to the above quote from the book?

6.2.5c9表示:涉及无符号的操作数可以从不溢出,因为这不能重新由所得无符号整型psented $ P $一个结果是减少的一种计算模是大于一的数可再通过将得到的式psented $ p $的最大值。

6.2.5c9 says: 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.

是不是这样?还是有别的东西我失踪?

Is that it? Or is there something else I'm missing?

推荐答案

是的,6.2.5c9正是你寻找的段落。

Yes, 6.2.5c9 is exactly the paragraph that you looked for.

这篇关于C:无符号运算元减运算符的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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