Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

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

  ...  
}
like image 642
Pablitorun Avatar asked Mar 21 '13 14:03

Pablitorun


People also ask

What is the difference between bitwise and logical?

Difference Between Bitwise and Logical Operators First, logical operators work on boolean expressions and return boolean values (either true or false), whereas bitwise operators work on binary digits of integer values (long, int, short, char, and byte) and return an integer.

What is bitwise and VS logical AND?

The logical AND operator works on Boolean expressions, and returns Boolean values only. The bitwise AND operator works on integer, short int, long, unsigned int type data, and also returns that type of data.

Is && bitwise or logical?

A Bitwise And operator is represented as '&' and a logical operator is represented as '&&'.

What is the difference between bitwise not and logical not?

“Logical not or !” is meant for boolean values and “bitwise not or ~” is for integers. Languages like C/C++ and python do auto promotion of boolean to integer type when an integer operator is applied.

What is the difference between logical AND bitwise AND&&operators?

The logical AND operator works on Boolean expressions, and returns Boolean values only. The bitwise AND operator works on integer, short int, long, unsigned int type data, and also returns that type of data. The && operator does not evaluate second operand if first operand becomes false.

What is the difference between bit-wise and and logical and in JavaScript?

As we know the bit-wise AND is represented as ‘&’ and the logical operator is represented as ‘&&’. There are some fundamental differences between them. These are as follows − The logical AND operator works on Boolean expressions, and returns Boolean values only.

What are the logical AND bitwise operators in VB NET?

Logical and Bitwise Operators in Visual Basic 1 Unary Logical Operator. The Not Operator performs logical negation on a Boolean expression. ... 2 Binary Logical Operators. The And Operator performs logical conjunction on two Boolean expressions. ... 3 Short-Circuiting Logical Operations. ... 4 Bitwise Operations. ...

What is the Order of the bitwise and shift operators?

The following list orders bitwise and shift operators starting from the highest precedence to the lowest: 1 Bitwise complement operator ~ 2 Shift operators << and >> 3 Logical AND operator & 4 Logical exclusive OR operator ^ 5 Logical OR operator | More ...


2 Answers

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.

like image 136
unwind Avatar answered Oct 28 '22 17:10

unwind


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.

like image 7
Russell Zahniser Avatar answered Oct 28 '22 16:10

Russell Zahniser