[英]C++ and left/right shift operators on a boolean
Consider the following program that tests the result of shift operators on boolean converted back to a boolean:考虑以下程序,该程序测试布尔值转换为布尔值的移位运算符的结果:
#include <iostream>
int main()
{
std::cout<<"right shift: bool/bool"<<std::endl;
std::cout<<"false << false = "<<bool(false << false)<<std::endl;
std::cout<<"false << true = "<<bool(false << true)<<std::endl;
std::cout<<"true << false = "<<bool(true << false)<<std::endl;
std::cout<<"true << true = "<<bool(true << true)<<std::endl;
std::cout<<std::endl;
std::cout<<"right shift: bool/unsigned int"<<std::endl;
std::cout<<"false << 0U = "<<bool(false << 0U)<<std::endl;
std::cout<<"false << 1U = "<<bool(false << 1U)<<std::endl;
std::cout<<"false << 2U = "<<bool(false << 2U)<<std::endl;
std::cout<<"false << 100U = "<<bool(false << 100U)<<std::endl;
std::cout<<"true << 0U = "<<bool(true << 0U)<<std::endl;
std::cout<<"true << 1U = "<<bool(true << 1U)<<std::endl;
std::cout<<"true << 2U = "<<bool(true << 2U)<<std::endl;
std::cout<<"true << 100U = "<<bool(true << 100U)<<std::endl;
std::cout<<std::endl;
std::cout<<"right shift: bool/int"<<std::endl;
std::cout<<"false << -100 = "<<bool(false << -100)<<std::endl;
std::cout<<"false << -2 = "<<bool(false << -2)<<std::endl;
std::cout<<"false << -1 = "<<bool(false << -1)<<std::endl;
std::cout<<"false << 0 = "<<bool(false << 0)<<std::endl;
std::cout<<"false << 1 = "<<bool(false << 1)<<std::endl;
std::cout<<"false << 2 = "<<bool(false << 2)<<std::endl;
std::cout<<"false << 100 = "<<bool(false << 100)<<std::endl;
std::cout<<"true << -100 = "<<bool(true << -100)<<std::endl;
std::cout<<"true << -2 = "<<bool(true << -2)<<std::endl;
std::cout<<"true << -1 = "<<bool(true << -1)<<std::endl;
std::cout<<"true << 0 = "<<bool(true << 0)<<std::endl;
std::cout<<"true << 1 = "<<bool(true << 1)<<std::endl;
std::cout<<"true << 2 = "<<bool(true << 2)<<std::endl;
std::cout<<"true << 100 = "<<bool(true << 100)<<std::endl;
std::cout<<std::endl;
std::cout<<"left shift: bool/bool"<<std::endl;
std::cout<<"false >> false = "<<bool(false >> false)<<std::endl;
std::cout<<"false >> true = "<<bool(false >> true)<<std::endl;
std::cout<<"true >> false = "<<bool(true >> false)<<std::endl;
std::cout<<"true >> true = "<<bool(true >> true)<<std::endl;
std::cout<<std::endl;
std::cout<<"left shift: bool/unsigned int"<<std::endl;
std::cout<<"false >> 0U = "<<bool(false >> 0U)<<std::endl;
std::cout<<"false >> 1U = "<<bool(false >> 1U)<<std::endl;
std::cout<<"false >> 2U = "<<bool(false >> 2U)<<std::endl;
std::cout<<"false >> 100U = "<<bool(false >> 100U)<<std::endl;
std::cout<<"true >> 0U = "<<bool(true >> 0U)<<std::endl;
std::cout<<"true >> 1U = "<<bool(true >> 1U)<<std::endl;
std::cout<<"true >> 2U = "<<bool(true >> 2U)<<std::endl;
std::cout<<"true >> 100U = "<<bool(true >> 100U)<<std::endl;
std::cout<<std::endl;
std::cout<<"left shift: bool/int"<<std::endl;
std::cout<<"false >> -100 = "<<bool(false >> -100)<<std::endl;
std::cout<<"false >> -2 = "<<bool(false >> -2)<<std::endl;
std::cout<<"false >> -1 = "<<bool(false >> -1)<<std::endl;
std::cout<<"false >> 0 = "<<bool(false >> 0)<<std::endl;
std::cout<<"false >> 1 = "<<bool(false >> 1)<<std::endl;
std::cout<<"false >> 2 = "<<bool(false >> 2)<<std::endl;
std::cout<<"false >> 100 = "<<bool(false >> 100)<<std::endl;
std::cout<<"true >> -100 = "<<bool(true >> -100)<<std::endl;
std::cout<<"true >> -2 = "<<bool(true >> -2)<<std::endl;
std::cout<<"true >> -1 = "<<bool(true >> -1)<<std::endl;
std::cout<<"true >> 0 = "<<bool(true >> 0)<<std::endl;
std::cout<<"true >> 1 = "<<bool(true >> 1)<<std::endl;
std::cout<<"true >> 2 = "<<bool(true >> 2)<<std::endl;
std::cout<<"true >> 100 = "<<bool(true >> 100)<<std::endl;
std::cout<<std::endl;
return 0;
}
It produces the following output on my computer:它在我的电脑上产生以下输出:
right shift: bool/bool
false << false = 0 // Well defined
false << true = 0 // Well defined
true << false = 1 // Well defined
true << true = 1 // Well defined
right shift: bool/unsigned int
false << 0U = 0 // Well defined
false << 1U = 0 // Well defined
false << 2U = 0 // ?
false << 100U = 0 // Undefined behaviour
true << 0U = 1 // Well defined
true << 1U = 1 // Well defined
true << 2U = 1 // ?
true << 100U = 0 // Undefined behaviour
right shift: bool/int
false << -100 = 0 // Undefined behaviour
false << -2 = 0 // Undefined behaviour
false << -1 = 0 // Undefined behaviour
false << 0 = 0 // Well defined
false << 1 = 0 // Well defined
false << 2 = 0 // ?
false << 100 = 0 // Undefined behaviour
true << -100 = 0 // Undefined behaviour
true << -2 = 0 // Well defined
true << -1 = 0 // Well defined
true << 0 = 1 // Well defined
true << 1 = 1 // Well defined
true << 2 = 1 // ?
true << 100 = 0 // Undefined behaviour
left shift: bool/bool
false >> false = 0 // Well defined
false >> true = 0 // Well defined
true >> false = 1 // Well defined
true >> true = 0 // Well defined
left shift: bool/unsigned int
false >> 0U = 0 // Well defined
false >> 1U = 0 // Well defined
false >> 2U = 0 // ?
false >> 100U = 0 // Undefined behaviour
true >> 0U = 1 // Well defined
true >> 1U = 0 // Well defined
true >> 2U = 0 // ?
true >> 100U = 0 // Undefined behaviour
left shift: bool/int
false >> -100 = 0 // Undefined behaviour
false >> -2 = 0 // Undefined behaviour
false >> -1 = 0 // Undefined behaviour
false >> 0 = 0 // Well defined
false >> 1 = 0 // Well defined
false >> 2 = 0 // ?
false >> 100 = 0 // Undefined behaviour
true >> -100 = 0 // Undefined behaviour
true >> -2 = 1 // Undefined behaviour
true >> -1 = 1 // Undefined behaviour
true >> 0 = 1 // Well defined
true >> 1 = 0 // Well defined
true >> 2 = 0 // ?
true >> 100 = 0 // Undefined behaviour
The C++ standard part 5.8 expr.shift
describes the behaviour of the shift operator, but I want to be sure to understand it well. C++ 标准部分 5.8
expr.shift
描述了移位运算符的行为,但我想确保很好地理解它。 Consequently, I would like to know for each line I test, whether the line is "well defined behaviour and implementation independent", or whether it's "implementation dependent/undefined behaviour".因此,我想知道对于我测试的每一行,该行是“明确定义的行为和实现无关”,还是“依赖于实现/未定义的行为”。
EDIT: I pre-filled it, so I just need a confirmation, and an answer about the case where I don't know (noted ?
)编辑:我预先填写了它,所以我只需要确认,以及关于我不知道的情况的答案(注意
?
)
There are only a few cases to consider.只有少数情况需要考虑。 We call
E1
the promoted left operand and E2
the promoted right operand.我们称
E1
为提升的左操作数,而E2
为提升的右操作数。 The undefined behavior cases are:未定义的行为案例是:
E2
"is negative, or greater than or equal to the length in bits of" E1
E2
“为负,或大于或等于” E1
位长度E1 << E2
, if E1
is signed and eitherE1 << E2
,如果E1
已签名并且 The implementation-defined behavior cases are:实现定义的行为案例是:
E1 >> E2
, if E1
"has a signed type and a negative value."E1 >> E2
,如果E1
“具有有符号类型和负值”。 Everything else is well-defined.其他一切都是明确定义的。
Your cases marked with a ?
您的案例标有
?
are all well-defined.都是明确定义的。
E1
and E2
are never negative, and E2
is nowhere near 32, so there's no concern for overflow. E1
和E2
从不为负,而E2
远不及 32,因此无需担心溢出。 All are well-defined int
s.所有都是明确定义的
int
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.