Tuesday, July 14, 2020

Difference between | and || pipe operators

Todays post is about the behaviour of | (pipe) and || (also pipe) operators.

| : It's a bitwise OR operation. It's modifying things at a binary level.

                   011                           3
in binary: | 100     in decimal:  |  4
                   ___                         ___
                   111                            7


The output of bitwise OR on two bits is 1 if either bit is 1 or 0 if both bits are 0. Bitwise OR on two numbers just does a bitwise OR on each bit individually.

|| : is the logical OR operator.It's used in conditional statements such as if, while, etc.

condition1 || condition2

Evaluates to true if either condition1 OR condition2 is true.

So to sum up :

One is a "bitwise or".

10011b | 01000b => 11011b

The other is a logic or.

true or false => true

No comments:

Post a Comment