或、与运算符
新手问题。 如何计算公式 A f B 的值,其中 f - 二元函数 OR 或 AND?
Newbie question.
How to calculate the value of the formula A f B, where f - the binary function OR or AND?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
条件运算符 && 之间是有区别的。和||以及布尔运算符 &和|。主要是优先级的差异(首先评估哪些运算符)以及 && 。和||正在‘逃避’。这意味着这是一个序列,例如...
如果 cond1 为 false,则 cond2 或 cond3 都不会被求值,因为代码正确地假设无论它们的值是什么,表达式都不能为 true。同样...
如果 cond1 为 true,则 cond2 或 cond3 都不会被求值,因为无论它们的值是什么,表达式都必须为 true。
按位对应, &和 |没有逃避。
希望有帮助。
There is a distinction between the conditional operators && and || and the boolean operators & and |. Mainly it is a difference of precendence (which operators get evaluated first) and also the && and || are 'escaping'. This means that is a sequence such as...
If cond1 is false, neither cond2 or cond3 are evaluated as the code rightly assumes that no matter what their value, the expression cannot be true. Likewise...
If cond1 is true, neither cond2 or cond3 are evaluated as the expression must be true no matter what their value is.
The bitwise counterparts, & and | are not escaping.
Hope that helps.
逻辑或为
||
,逻辑与为&&
。如果您需要否定 NOT,请在表达式前加上
!
前缀。示例:
当 A 和 B 为真或 C 为真或 D 不为真(即假)时,X 将为真。
如果您想要按位与/或/非,您可以使用
&
、|
和~
。但如果您正在处理布尔/真值,则您不想使用它们。例如,由于按位运算的工作方式,它们不提供短路评估。Logical OR is
||
, logical AND is&&
.If you need the negation NOT, prefix your expression with
!
.Example:
Then X will be true when either A and B are true or if C is true or if D is not true (i.e. false).
If you wanted bit-wise AND/OR/NOT, you would use
&
,|
and~
. But if you are dealing with boolean/truth values, you do not want to use those. They do not provide short-circuit evaluation for example due to the way a bitwise operation works.我不确定这是否能回答您的问题,但例如:
I'm not sure if this answers your question, but for example:
使用“&&”对于 AND 并使用“||”对于 OR,例如:
Use '&&' for AND and use '||' for OR, for example:
如果您感兴趣的是按位运算,请在此处查看简短教程:http://weblogs.asp.net/alessandro/archive/2007/10/02/bitwise-operators-in-c-or-xor- and-amp-amp-not.aspx .bitwise 操作执行与上面示例相同的操作,它们只是使用二进制表示(该操作适用于值的每个单独位)
如果您想要逻辑运算答案是已经给了。
If what interests you is bitwise operations look here for a brief tutorial : http://weblogs.asp.net/alessandro/archive/2007/10/02/bitwise-operators-in-c-or-xor-and-amp-amp-not.aspx .bitwise operation perform the same operations like the ones exemplified above they just work with binary representation (the operation applies to each individual bit of the value)
If you want logical operation answers are already given.
&&仅当两个操作数都为 true 时,该操作才会返回 true,这意味着
||如果一个或两个操作数为 true,则它的操作返回 true,这意味着
如果您
在结果中写入,则有
因此,我上面写的内容用于每对位
&& it's operation return true only if both operand it's true which implies
|| it's operation return true if one or both operand it's true which implies
if You write
in result you have
Therefore, the which I wrote above is used with respect to every pair of bits
上面有很多答案,我将尝试一种不同的方式:
如果您正在寻找按位运算,则仅使用其中一个标记,例如:
3 & 1 //==1 - 和
4 | 1 //==5 - 或
many answers above, i will try a different way:
if you are looking for bitwise operations use only one of the marks like:
3 & 1 //==1 - and
4 | 1 //==5 - or