什么是& b? [英] What's the result of a & b?

查看:251
本文介绍了什么是& b?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这很尴尬,但是在C ++标准中定义了位运算符如下(强调我的)。


转换; 结果是其操作数的按位AND函数。运算符仅适用于整数或无范围的枚举操作数。


这看起来对我来说没有意义。



我知道AND函数是很好理解的,因此可能不会需要解释。单词逐位的含义也应该相当清楚:该函数应用于其操作数的相应位。

解决方案



/ div>

这是低于指定的。



例如 www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1857rel =nofollow>缺陷报告1857:关于位的其他问题:


5.11 [expr.bit.and],
5.12 [expr.xor]和5.13 [expr.or]中按位操作的规范使用未定义



这部分解决方案可能会导致这种情况的部分原因。定义bit(这是
,否则在C ++中当前未定义)作为
2的给定幂的值。


,响应为:


CWG决定重新制定操作的说明
自身以避免引用分割出更大的
定义bit等的问题,以便为进一步的
考虑发布1943.


缺陷报告1943 说:


CWG在2014-06(Rapperswil)会议上决定只处理问题所提出的有限子集这个
问题是剩余问题的占位符,例如根据值2 n 定义
位,指定是否使用位元,字段有一个
符号位等。


我们可以从缺陷报告1796:空字符的全位零是有意义的要求吗?这个问题的标准是什么意思,当它指的位影响/影响其他部分:


根据2.3 [lex.charset]第3段


基本执行字符集和基本执行宽字符集应包含基本$ b $的所有成员b源字符集,加上表示警报,
退格和回车的控制字符,加上空字符(分别为
空宽字符),其表示全部为零。


不清楚可移植程序可以检查
表示的位;相反,它似乎限于检查
对应于值表示
(3.9.1 [basic.fundamental]段落1)的数字的位。可能更适当的是
要求空字符值比较等于0或'\0'
,而不是指定表示的位模式。



对于shift,bitwise和,和
位或者操作符的定义有一个类似的问题:对表示的位
模式的规范约束,或者从
将这些模式解释为数字?


在这种情况下,决议更改为:


表示全部为零位


到:


值为0。


请注意,在ecatmur的回答中提到的C ++标准草案遵循C标准部分 5.2.4.2.1 3 中的 3.9.1 [basic.fundamental]没有引用C标准中的 6.5 / 4 部分,这将至少告诉我们结果是实现定义的。我在下面的评论中解释了C ++标准只能合并来自规范引用的文本。


This is awkward, but the bitwise AND operator is defined in the C++ standard as follows (emphasis mine).

The usual arithmetic conversions are performed; the result is the bitwise AND function of its operands. The operator applies only to integral or unscoped enumeration operands.

This looks kind of meaningless to me. The "bitwise AND function" is not defined anywhere in the standard, as far as I can see.

I get that the AND function is well-understood and thus may not require explanation. The meaning of the word "bitwise" should also be rather clear: the function is applied to corresponding bits of its operands. However, what constitute the bits of the operands is not clear.

What gives?

解决方案

This is underspecified. The issue of what the standard means when it refers to bit-wise operations is the subject of a few defect reports.

For example defect report 1857: Additional questions about bits:

The specification of the bitwise operations in 5.11 [expr.bit.and], 5.12 [expr.xor], and 5.13 [expr.or] uses the undefined term "bitwise" in describing the operations, without specifying whether it is the value or object representation that is in view.

Part of the resolution of this might be to define "bit" (which is otherwise currently undefined in C++) as a value of a given power of 2.

and the response was:

CWG decided to reformulate the description of the operations themselves to avoid references to bits, splitting off the larger questions of defining "bit" and the like to issue 1943 for further consideration.

and defect report 1943 says:

CWG decided at the 2014-06 (Rapperswil) meeting to address only a limited subset of the questions raised by issues 1857 and 1861. This issue is a placeholder for the remaining questions, such as defining a "bit" in terms of a value of 2n, specifying whether a bit-field has a sign bit, etc.

We can see from this defect report 1796: Is all-bits-zero for null characters a meaningful requirement?, that this issue of what the standard means when it refers to bits affected/affects other sections as well:

According to 2.3 [lex.charset] paragraph 3,

The basic execution character set and the basic execution wide-character set shall each contain all the members of the basic source character set, plus control characters representing alert, backspace, and carriage return, plus a null character (respectively, null wide character), whose representation has all zero bits.

It is not clear that a portable program can examine the bits of the representation; instead, it would appear to be limited to examining the bits of the numbers corresponding to the value representation (3.9.1 [basic.fundamental] paragraph 1). It might be more appropriate to require that the null character value compare equal to 0 or '\0' rather than specifying the bit pattern of the representation.

There is a similar issue for the definition of shift, bitwise and, and bitwise or operators: are those specifications constraints on the bit pattern of the representation or on the values resulting from the interpretation of those patterns as numbers?

In this case the resolution was to change:

representation has all zero bits

to:

value is 0.

Note that as mentioned in ecatmur's answer the draft C++ standard does defer to C standard section 5.2.4.2.1 in section 3.9.1 [basic.fundamental] in paragraph 3 it does not refer to section 6.5/4 from the C standard which would at least tell us that the results are implementation defined. I explain in my comment below that C++ standard can only incorporate text from normative references explicitly.

这篇关于什么是& b?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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