tags:

views:

2257

answers:

10

I have always used || (two pipes) in OR expressions, both in C# and PHP. Occasonally 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?

+16  A: 

One is a "bitwise or".

10011b | 01000b => 11011b

The other is a logic or.

true or false => true

nsanders
A: 

The | operator performs a bitwise OR of its two operands (meaning both sides must evaluate to false for it to return false) while the || operator will only evaluate the second operator if it needs to.

http://msdn.microsoft.com/en-us/library/kxszd0kx(VS.71).aspx

http://msdn.microsoft.com/en-us/library/6373h346(VS.71).aspx

homeskillet
If you actually read those articles, you would have seen that they are referring to bitwise operators
johnc
A: 

The singe pipe "|" is the "bitwise" or and should only be used when you know what you're doing. The double pipe "||" is a logical or, and can be used in logical statements, like "x == 0 || x == 1".

Here's an example of what the bitwise or does: if a=0101 and b=0011, then a|b=0111. If you're dealing with a logic system that treats any non-zero as true, then the bitwise or will act in the same way as the logical or, but it's counterpart (bitwise and, "&") will NOT. Also the bitwise or does not perform short circuit evaluation.

Kyle Cronin
A: 

A single pipe (|) is the bitwise OR operator.

Two pipes (||) is the logical OR operator.

They are not interchangeable.

Dane
A: 

|| (two pipes) is usually a logical or while | (one pipe) is a binary or. Off the top of my head, I can't think of any time the difference would be a big gotcha (other than when you're assigning the result to something else). However I sure someone else will have a situation where it matters.

Edit: Wow, six other answers in the time it took me to write this.

David Locke
+19  A: 

|| 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. Its 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.

Evan Shaw
+46  A: 

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 the 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.

Michael Stum
not sure why this is getting downvoted -- it is correct! Double check it in your compiler if you don't believe him!
Jeff Atwood
Yeah, I didn't believe it until I created a console app - but good lord! Why would they give you the rope to hang yourself! I hated that about VB.NET - the OrElse and AndAlso keywords!
Jarrod Dixon
Love the short-circuit nature of these commands! I use this all the time to only execute the second check if the first succeeds, a null-object check for example:if(myObj != null
joshperry
Of course maybe I should read the whole answer before I duplicate part of it!
joshperry
BlueRaja - Danny Pflughoeft
It's not a hack, it's officially specified for C# in Section 7.10.3 Boolean logical operators: "The result of x | y is true if either x or y is true. Otherwise, the result is false." Also see Section 7.11 Conditional logical operators: "The operation x || y corresponds to the operation x | y, except that y is evaluated only if x is false." which further "legalizes" | as a conditional operator. And people whoever will use C/C++ will be in trouble anyway if they just blindly assume that stuff works equally. As said: Using | in an if statement is a design smell, but a perfectly legal operation.
Michael Stum
+2  A: 

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. E.g.

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

Trevor Abell
+1  A: 

The single pipe, |, is one of the bitwise operators.

From Wikipedia:

In the C programming language family, the bitwise OR operator is "|" (pipe). Again, this operator must not be confused with its Boolean "logical or" counterpart, which treats its operands as Boolean values, and is written "||" (two pipes).

codeLes
A: 

Bitwise (|) vs. logical(||)! Think of logical as the Comparable objects in Java, comparing some distinguishable "parts" while the bitwise operator looks at these objects and instead of seeing if they are visually twins (like logical does), does a DNA sample and looks at the 0's and 1's instead.

mduvall