Pablitorun
Pablitorun

Reputation: 1025

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);

  ...  
}

Upvotes: 7

Views: 2986

Answers (6)

Mike
Mike

Reputation: 49363

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.

Upvotes: 3

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272447

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]. Logical OR is guaranteed to give you 0 or 1.

For this reason, you should prefer logical OR. Your intent is (presumably) to manipulate logical values, so using a non-logical operator is illogical.*


* Pun definitely intended.

Upvotes: 8

Brian Cain
Brian Cain

Reputation: 14619

Is there any functional difference between logical and bitwise operators in the following case?

Yes, there is (lazy eval as others have pointed out).

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.

Upvotes: 1

Ivaylo Strandjev
Ivaylo Strandjev

Reputation: 70931

The bitwise or operator never short-circuits while the logical one does. That is if bFoo is true, bBar is never evaluated.

Upvotes: 1

unwind
unwind

Reputation: 399703

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.

Upvotes: 10

Russell Zahniser
Russell Zahniser

Reputation: 16354

The boolean || will short-circuit: if the first operand is true, the second will never be evaluated. In contrast, the bitwise | always evaluates both arguments.

Upvotes: 7

Related Questions