简体   繁体   English

按位OR和逻辑OR运算符。 有什么不同?

[英]Bitwise OR and logical OR operators. What's the difference?

Is there any functional difference between logical and bitwise operators in the following code? 以下代码中的逻辑运算符和位运算符之间是否存在任何功能差异? What are the reasons to use one or another? 使用这一个或另一个的原因是什么?

typedef unsigned char BOOLEAN;

void orOperatorsComparison(BOOLEAN bBar, BOOLEAN bFoo)
{
  BOOLEAN bLogicalOr = (bFoo || bBar);
  BOOLEAN bBitwiseOr = (bFoo | bBar);

  ...  
}

What does "support" mean? “支持”是什么意思?

If it's a logical or that you mean, then of course you should always use || 如果它是合乎逻辑的或你的意思,那么当然你应该总是使用|| since that is the Boolean, logical, "or" operator. 因为那是布尔,逻辑,“或”运算符。

It has the benefit of being able to short-circuit, but that won't matter much in code this simple. 它具有能够短路的优点,但这在代码中并不重要,这很简单。

I would consider it odd and weird (and due for correcting) if bitwise or was being used when the point is not to manipulate bits. 我认为它是奇怪的和奇怪的(并且由于纠正)如果按位或当点不操作位时使用。

The boolean || 布尔值|| will short-circuit: if the first operand is true , the second will never be evaluated. 将短路:如果第一个操作数为true ,则第二个操作数将永远不会被评估。 In contrast, the bitwise | 相反,按位| always evaluates both arguments. 总是评估两个参数。

Other answers have already talked about short-circuiting (but that's not an issue in your particular code). 其他答案已经讨论过短路(但这不是您特定代码中的问题)。 But here is one key difference. 但这是一个关键的区别。

If, for some reason, your input values are not in [0,1], then a bitwise OR will give you an answer that may also not be in [0,1]. 如果由于某种原因,您的输入值不在[0,1]中,那么按位OR将给出一个也可能不在[0,1]中的答案。 Logical OR is guaranteed to give you 0 or 1. 逻辑OR 保证给你0或1。

For this reason, you should prefer logical OR. 因此,您应该更喜欢逻辑OR。 Your intent is (presumably) to manipulate logical values, so using a non-logical operator is illogical. 您的意图(可能)是操纵逻辑值,因此使用非逻辑运算符是不合逻辑的。 * *


* Pun definitely intended. *双关语绝对有意。

In that particular case, no, there is no difference in the result : 在那种特殊情况下,不, 结果没有区别:

1 || 0 == 1
1 | 0  == 1

all the truth tables apply here. 所有的真值表都适用于此。

If you're talking about how we got to the result then there could be a difference. 如果你在谈论我们如何得到结果,那么可能会有所不同。 With the || 随着|| you have a short circuit mechanism: 你有一个短路机制:

BOOLEAN bFooBar = (bFoo||bBar) // if bFoo is TRUE, we never look at bBar
                               // vs
BOOLEAN bFooBar = (bFoo|bBar)  // where we take into account both values

So the long and short of it is, yes you can use logical and bitwise operators incorrectly in some instances and get the same results, but why would you ever do that? 因此,长期和短期的它,是的,你可以不正确地在某些情况下使用逻辑和按位运算符,并得到相同的结果,但为什么你做吗? If you know it's wrong, and you know that it can lead to bad, hard to find bugs, use the tools the language gaves you for the jobs they were meant to do. 如果你知道这是错误的,并且你知道它可能会导致错误,很难找到错误,那么使用语言为你应该完成的工作提供的工具。

The bitwise or operator never short-circuits while the logical one does. 按位或运算符永远不会与逻辑运算符短路。 That is if bFoo is true, bBar is never evaluated. 那就是如果bFoo为真, bBar永远不会被评估。

Is there any functional difference between logical and bitwise operators in the following case? 在下列情况下,逻辑运算符和按位运算符之间是否存在任何功能差异?

Yes, there is (lazy eval as others have pointed out). 是的,有(其他人指出的懒惰eval)。

Any reason to support one or the other? 有理由支持其中一个吗?

If somehow they were equivalent, the case for using logical operators would be to preserve the semantic intended by the type. 如果它们在某种程度上是等价的,那么使用逻辑运算符的情况就是保留该类型所预期的语义。 See also: Principle of least astonishment . 另见: 最不惊讶的原则

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM