What is the difference between the | and || or operators?
Solution 1
Just like the &
and &&
operator, the double Operator is a "short-circuit" operator.
For example:
if(condition1 || condition2 || condition3)
If condition1 is true, condition 2 and 3 will NOT be checked.
if(condition1 | condition2 | condition3)
This will check conditions 2 and 3, even if 1 is already true. As your conditions can be quite expensive functions, you can get a good performance boost by using them.
There is one big caveat, NullReferences or similar problems. For example:
if(class != null && class.someVar < 20)
If class is null, the if-statement will stop after class != null
is false. If you only use &, it will try to check class.someVar
and you get a nice NullReferenceException
. With the Or-Operator that may not be that much of a trap as it's unlikely that you trigger something bad, but it's something to keep in mind.
No one ever uses the single &
or |
operators though, unless you have a design where each condition is a function that HAS to be executed. Sounds like a design smell, but sometimes (rarely) it's a clean way to do stuff. The &
operator does "run these 3 functions, and if one of them returns false, execute the else block", while the |
does "only run the else block if none return false" - can be useful, but as said, often it's a design smell.
There is a Second use of the |
and &
operator though: Bitwise Operations.
Solution 2
|| is the logical OR operator. It sounds like you basically know what that is. It's used in conditional statements such as if, while, etc.
condition1 || condition2
Evaluates to true if either condition1 OR condition2 is true.
| is the bitwise OR operator. It's used to operate on two numbers. You look at each bit of each number individually and, if one of the bits is 1 in at least one of the numbers, then the resulting bit will be 1 also. Here are a few examples:
A = 01010101
B = 10101010
A | B = 11111111
A = 00000001
B = 00010000
A | B = 00010001
A = 10001011
B = 00101100
A | B = 10101111
Hopefully that makes sense.
So to answer the last two questions, I wouldn't say there are any caveats besides "know the difference between the two operators." They're not interchangeable because they do two completely different things.
Solution 3
One is a "bitwise or".
10011b | 01000b => 11011b
The other is a logic or.
true or false => true
Solution 4
Good question. These two operators work the same in PHP and C#.
|
is a bitwise OR. It will compare two values by their bits. E.g. 1101 | 0010 = 1111. This is extremely useful when using bit options. E.g. Read = 01 (0X01) Write = 10 (0X02) Read-Write = 11 (0X03). One useful example would be opening files. A simple example would be:
File.Open(FileAccess.Read | FileAccess.Write); //Gives read/write access to the file
||
is a logical OR. This is the way most people think of OR and compares two values based on their truth. E.g. I am going to the store or I will go to the mall. This is the one used most often in code. For example:
if(Name == "Admin" || Name == "Developer") { //allow access } //checks if name equals Admin OR Name equals Developer
PHP Resource: http://us3.php.net/language.operators.bitwise
C# Resources: http://msdn.microsoft.com/en-us/library/kxszd0kx(VS.71).aspx
http://msdn.microsoft.com/en-us/library/6373h346(VS.71).aspx
Solution 5
& - (Condition 1 & Condition 2): checks both cases even if first one is false
&& - (Condition 1 && Condition 2): dosen't bother to check second case if case one is false
&& - operator will make your code run faster, professionally & is rarely used
| - (Condition 1 | Condition 2): checks both cases even if case 1 is true
|| - (Condition 1 || Condition 2): dosen't bother to check second case if first one is true
|| - operator will make your code run faster, professionally | is rarely used
GloryFish
Programmer, Objective-C, Python, PHP, Lua http://github.com/GloryFish/
Updated on July 19, 2020Comments
-
GloryFish almost 4 years
I have always used
||
(two pipes) in OR expressions, both in C# and PHP. Occasionally I see a single pipe used:|
. What is the difference between those two usages? Are there any caveats when using one over the other or are they interchangeable? -
johnc about 15 yearsIf you actually read those articles, you would have seen that they are referring to bitwise operators
-
Alex over 10 yearsIf you ignore the bitwise operation, double pipe is lazy evaluation and single pipe is greedy, within logical operator area.
-
ToolmakerSteve over 7 yearsFWIW, Technically, in C#
|
is a logical or when applied to booleans. As your linked reference states. In practice, the end result is the same as if it were a bitwise operator, because the bitwise values oftrue
andfalse
are such that a bitwise or of their values produces the exact same result as a logical or does. That is(int)(bool1 | bool2)
==((int)bool1) | ((int)bool2)
. -
Emaborsa almost 6 years
rarely used
? All depends on what you want or need to do. -
juharr over 5 years
|
can be used onbool
types as well without short circuiting. -
juharr over 5 yearsThat's not what bitwise means.
-
juharr over 5 years'|' can also be used on
bool
types without short circuiting. -
juharr over 5 yearsWhy would you give a java example for a question that doesn't even mention java?
-
Iannick over 5 yearsGreat! Short and sweet, I would remove the "| is rarely used" and "& is rarely used" because, as Emaborsa said, its really depends on what you want or need to do.
-
Donny V. almost 5 yearsThis helped me understand how someone was using bitwise OR operator to merge filters in Mongodb C# driver. gist.github.com/a3dho3yn/…
-
Liam Pillay almost 3 yearsFor anyone not familiar with bitwise operators, the result example is obtained because each bit row from each column is tested against the other using OR comparison. docs.microsoft.com/en-us/cpp/c-language/c-bitwise-operators
-
arkon almost 2 years"It's used to operate on two numbers" This is an inaccurate/incomplete description.
if (true | false)
is perfectly valid in C#. -
arkon almost 2 yearsI rely on bitwise operations quite heavily. These specific operators are also useful when dealing with flags. You're mistaking people rarely use them with I rarely use them.
-
Denis G. Labrecque almost 2 yearsIs rarely used means is rarely the appropriate operator, because in all possible cases an "or" operation should short-circuit.