This question already has an answer here:
这个问题在这里已有答案:
- The output of cout << 1 && 0; [closed] 1 answer
cout的输出<< 1 && 0; [关闭] 1回答
#include <iostream>
using namespace std;
int main()
{
int a = 8;
cout << "ANDing integer 'a' with 'true' :" << a && true;
return 0;
}
Why does this program output 8
and not 1
(for true
)?
为什么这个程序输出8而不是1(对于true)?
4 个解决方案
#1
8
If you look at operator precedence, you will find that the left-shift operator <<
has a higher precedence than the logical AND operator &&
.
如果查看运算符优先级,您会发现左移运算符< <的优先级高于逻辑and运算符&&。< p>
As such, it is evaluated as:
因此,它被评估为:
(cout << "ANDing integer 'a' with 'true' :" << a) && true;
which prints 8.
打印8。
#2
4
The logical AND operator, &&
, works with expressions.
The first expression is evaluated. If the first expression is false, no more evaluations. Otherwise the second expression is evaluated.
Next, the result of the two Boolean expressions is returned.
逻辑AND运算符&&与表达式一起使用。第一个表达式被评估。如果第一个表达式为false,则不再进行评估。否则,将评估第二个表达式。接下来,返回两个布尔表达式的结果。
So, the first expression is evaluated:
因此,评估第一个表达式:
cout << "ANDing integer 'a' with 'true' :" << a
A side effect of the evaluation is the following output to the console:
评估的副作用是控制台的以下输出:
ANDing integer 'a' with 'true' :8
Your output is generated because it is a side-effect of evaluating the first expression.
生成输出是因为它是评估第一个表达式的副作用。
#3
3
In the expression 8 && true
8 is evaluated first (and in this turn also streamed). Than true is evaluated and the operator returns true after that. But than the 8 is already in the stream. If you put (8 && true)
instead, the parentheses are evaluated first and the result is guarantreed to be 1, which then is streamed.
在表达式8中,&& true 8首先被评估(并且在此轮中也被流式传输)。评估为真,然后运算符返回true。但是比8已经在流中了。如果改为放入(8 && true),则首先计算括号,结果保证为1,然后进行流式处理。
This behavior is called Operator Precedence - which describes the order in which the operators are executed (in your case &&
and <<
).
此行为称为“运算符优先级” - 它描述了运算符的执行顺序(在您的情况下为&&和<<)。
#4
2
The logical AND operator &&
works with expressions. If both are true returns true and false if otherwise.
逻辑AND运算符&&使用表达式。如果两者都为真则返回true,否则返回false。
cout << "ANDing integer 'a' with 'true' :" << a && true;
is being evaluated as:
被评估为:
(cout << "ANDing integer 'a' with 'true' :" << a) && true;
due to operator precedence (<<
having higher precedence than AND &&
) giving you the print of: 8
if we do the following change: (a && true);
it will give you the print of: 1
由于运算符优先级(< <具有比and &&更高的优先级),如果我们进行以下更改,则给出打印:8;(a && true);它会给你打印:1< p>
#1
8
If you look at operator precedence, you will find that the left-shift operator <<
has a higher precedence than the logical AND operator &&
.
如果查看运算符优先级,您会发现左移运算符< <的优先级高于逻辑and运算符&&。< p>
As such, it is evaluated as:
因此,它被评估为:
(cout << "ANDing integer 'a' with 'true' :" << a) && true;
which prints 8.
打印8。
#2
4
The logical AND operator, &&
, works with expressions.
The first expression is evaluated. If the first expression is false, no more evaluations. Otherwise the second expression is evaluated.
Next, the result of the two Boolean expressions is returned.
逻辑AND运算符&&与表达式一起使用。第一个表达式被评估。如果第一个表达式为false,则不再进行评估。否则,将评估第二个表达式。接下来,返回两个布尔表达式的结果。
So, the first expression is evaluated:
因此,评估第一个表达式:
cout << "ANDing integer 'a' with 'true' :" << a
A side effect of the evaluation is the following output to the console:
评估的副作用是控制台的以下输出:
ANDing integer 'a' with 'true' :8
Your output is generated because it is a side-effect of evaluating the first expression.
生成输出是因为它是评估第一个表达式的副作用。
#3
3
In the expression 8 && true
8 is evaluated first (and in this turn also streamed). Than true is evaluated and the operator returns true after that. But than the 8 is already in the stream. If you put (8 && true)
instead, the parentheses are evaluated first and the result is guarantreed to be 1, which then is streamed.
在表达式8中,&& true 8首先被评估(并且在此轮中也被流式传输)。评估为真,然后运算符返回true。但是比8已经在流中了。如果改为放入(8 && true),则首先计算括号,结果保证为1,然后进行流式处理。
This behavior is called Operator Precedence - which describes the order in which the operators are executed (in your case &&
and <<
).
此行为称为“运算符优先级” - 它描述了运算符的执行顺序(在您的情况下为&&和<<)。
#4
2
The logical AND operator &&
works with expressions. If both are true returns true and false if otherwise.
逻辑AND运算符&&使用表达式。如果两者都为真则返回true,否则返回false。
cout << "ANDing integer 'a' with 'true' :" << a && true;
is being evaluated as:
被评估为:
(cout << "ANDing integer 'a' with 'true' :" << a) && true;
due to operator precedence (<<
having higher precedence than AND &&
) giving you the print of: 8
if we do the following change: (a && true);
it will give you the print of: 1
由于运算符优先级(< <具有比and &&更高的优先级),如果我们进行以下更改,则给出打印:8;(a && true);它会给你打印:1< p>