<p>I have always used <code>||</code> (two pipes) in OR expressions, both in C# and PHP. Occasionally I see a single pipe used: <code>|</code>. What is the difference between those two usages? Are there any caveats when using one over the other or are they interchangeable?</p>

[{'answer_id': 35303, 'author': 'nsanders', 'author_id': 1244, 'author_profile': 'https://Stackoverflow.com/users/1244', 'pm_score': 5, 'selected': False, 'text': '<p>One is a "bitwise or".</p>\n\n<blockquote>\n  <p>10011b | 01000b => 11011b</p>\n</blockquote>\n\n<p>The other is a logic or.</p>\n\n<blockquote>\n  <p>true or false => true</p>\n</blockquote>\n'}, {'answer_id': 35306, 'author': 'homeskillet', 'author_id': 3400, 'author_profile': 'https://Stackoverflow.com/users/3400', 'pm_score': -1, 'selected': False, 'text': '<p>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.</p>\n\n<p><a href="http://msdn.microsoft.com/en-us/library/kxszd0kx(VS.71).aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/kxszd0kx(VS.71).aspx</a></p>\n\n<p><a href="http://msdn.microsoft.com/en-us/library/6373h346(VS.71).aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/6373h346(VS.71).aspx</a></p>\n'}, {'answer_id': 35308, 'author': 'Kyle Cronin', 'author_id': 658, 'author_profile': 'https://Stackoverflow.com/users/658', 'pm_score': -1, 'selected': False, 'text': '<p>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".</p>\n\n<p>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, "&amp;") will NOT. Also the bitwise or does not perform short circuit evaluation.</p>\n'}, {'answer_id': 35311, 'author': 'Dane', 'author_id': 2929, 'author_profile': 'https://Stackoverflow.com/users/2929', 'pm_score': -1, 'selected': False, 'text': '<p>A single pipe (|) is the <a href="http://en.wikipedia.org/wiki/Bitwise_operation" rel="nofollow noreferrer">bitwise OR operator</a>.</p>\n\n<p>Two pipes (||) is the logical OR operator.</p>\n\n<p>They are not interchangeable.</p>\n'}, {'answer_id': 35313, 'author': 'Evan Shaw', 'author_id': 510, 'author_profile': 'https://Stackoverflow.com/users/510', 'pm_score': 6, 'selected': False, 'text': '<p>|| 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.</p>\n\n<pre><code>condition1 || condition2\n</code></pre>\n\n<p>Evaluates to true if either condition1 OR condition2 is true.</p>\n\n<p>| 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:</p>\n\n<pre><code>A = 01010101\nB = 10101010\nA | B = 11111111\n\nA = 00000001\nB = 00010000\nA | B = 00010001\n\nA = 10001011\nB = 00101100\n\nA | B = 10101111\n</code></pre>\n\n<p>Hopefully that makes sense.</p>\n\n<p>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.</p>\n'}, {'answer_id': 35314, 'author': 'Michael Stum', 'author_id': 91, 'author_profile': 'https://Stackoverflow.com/users/91', 'pm_score': 10, 'selected': True, 'text': '<p>Just like the <code>&amp;</code> and <code>&amp;&amp;</code> operator, the double Operator is a "short-circuit" operator.</p>\n\n<p>For example:</p>\n\n<pre><code>if(condition1 || condition2 || condition3)\n</code></pre>\n\n<p>If condition1 is true, condition 2 and 3 will NOT be checked.</p>\n\n<pre><code>if(condition1 | condition2 | condition3)\n</code></pre>\n\n<p>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.</p>\n\n<p>There is one big caveat, NullReferences or similar problems. For example:</p>\n\n<pre><code>if(class != null &amp;&amp; class.someVar &lt; 20)\n</code></pre>\n\n<p>If class is null, the if-statement will stop after <code>class != null</code> is false. If you only use &amp;, it will try to check <code>class.someVar</code> and you get a nice <code>NullReferenceException</code>. 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.</p>\n\n<p>No one ever uses the single <code>&amp;</code> or <code>|</code> 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 <code>&amp;</code> operator does "run these 3 functions, and if one of them returns false, execute the else block", while the <code>|</code> does "only run the else block if none return false" - can be useful, but as said, often it\'s a design smell.</p>\n\n<p>There is a Second use of the <code>|</code> and <code>&amp;</code> operator though: <a href="http://www.c-sharpcorner.com/UploadFile/chandrahundigam/BitWiserOpsInCS11082005050940AM/BitWiserOpsInCS.aspx" rel="noreferrer">Bitwise Operations</a>.</p>\n'}, {'answer_id': 35331, 'author': 'Trevor Abell', 'author_id': 2916, 'author_profile': 'https://Stackoverflow.com/users/2916', 'pm_score': 4, 'selected': False, 'text': '<p>Good question.  These two operators work the same in PHP and C#.</p>\n\n<p><code>|</code> 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:  </p>\n\n<pre><code>File.Open(FileAccess.Read | FileAccess.Write);  //Gives read/write access to the file\n</code></pre>\n\n<p><code>||</code> 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:</p>\n\n<pre><code>if(Name == "Admin" || Name == "Developer") { //allow access } //checks if name equals Admin OR Name equals Developer\n</code></pre>\n\n<p>PHP Resource: <a href="http://us3.php.net/language.operators.bitwise" rel="noreferrer">http://us3.php.net/language.operators.bitwise</a></p>\n\n<p>C# Resources: <a href="http://msdn.microsoft.com/en-us/library/kxszd0kx(VS.71).aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/kxszd0kx(VS.71).aspx</a></p>\n\n<p><a href="http://msdn.microsoft.com/en-us/library/6373h346(VS.71).aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/6373h346(VS.71).aspx</a></p>\n'}, {'answer_id': 35337, 'author': 'codeLes', 'author_id': 3030, 'author_profile': 'https://Stackoverflow.com/users/3030', 'pm_score': -1, 'selected': False, 'text': '<p>The single pipe, |, is one of the <a href="http://en.wikipedia.org/wiki/Bitwise_operation#OR" rel="nofollow noreferrer">bitwise</a> operators.</p>\n\n<p>From Wikipedia: </p>\n\n<blockquote>\n  <p>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).</p>\n</blockquote>\n'}, {'answer_id': 9835789, 'author': 'vishesh', 'author_id': 1219463, 'author_profile': 'https://Stackoverflow.com/users/1219463', 'pm_score': 3, 'selected': False, 'text': '<p>Simple example in java</p>\n\n<pre><code>public class Driver {\n\n  static int x;\n  static int y;\n\npublic static void main(String[] args) \nthrows Exception {\n\nSystem.out.println("using double pipe");\n    if(setX() || setY())\n        {System.out.println("x = "+x);\n        System.out.println("y = "+y);\n        }\n\n\n\nSystem.out.println("using single pipe");\nif(setX() | setY())\n    {System.out.println("x = "+x);\n    System.out.println("y = "+y);\n    }\n\n}\n\n static boolean setX(){\n      x=5;\n     return true;\n  }\n static boolean setY(){\n      y=5;\n      return true;\n  }\n}\n</code></pre>\n\n<p>output : </p>\n\n<pre><code>using double pipe\nx = 5\ny = 0\nusing single pipe\nx = 5\ny = 5\n</code></pre>\n'}, {'answer_id': 39677610, 'author': 'Dragos Bandur', 'author_id': 6874836, 'author_profile': 'https://Stackoverflow.com/users/6874836', 'pm_score': 0, 'selected': False, 'text': "<p>By their mathematical definition, OR and AND are binary operators; they verify the LHS and RHS conditions regardless, similarly to | and &amp;. </p>\n\n<p>|| and &amp;&amp; alter the properties of the OR and AND operators by stopping them when the LHS condition isn't fulfilled.</p>\n"}, {'answer_id': 47277418, 'author': 'Bhai Saab', 'author_id': 8917503, 'author_profile': 'https://Stackoverflow.com/users/8917503', 'pm_score': 4, 'selected': False, 'text': "<p>&amp; - (Condition 1 &amp; Condition 2): checks both cases even if first one is false</p>\n\n<p>&amp;&amp; - (Condition 1 &amp;&amp; Condition 2): dosen't bother to check second case if case one \n                                   is false </p>\n\n<p>&amp;&amp; - operator will make your code run faster, professionally &amp; is rarely used</p>\n\n<p>| - (Condition 1 | Condition 2): checks both cases even if case 1 is true</p>\n\n<p>|| - (Condition 1 || Condition 2): dosen't  bother to check second case if first \n                                   one is true </p>\n\n<p>|| - operator will make your code run faster, professionally | is rarely used</p>\n"}, {'answer_id': 62966167, 'author': 'RvSingh3213', 'author_id': 13728648, 'author_profile': 'https://Stackoverflow.com/users/13728648', 'pm_score': -1, 'selected': False, 'text': '<p>For bitwise <code>|</code> and Logicall <code>||</code></p>\n<p>OR</p>\n<p>bitwise <code>&amp;</code> and logicall <code>&amp;&amp;</code></p>\n<p>it means  <code> if( a&gt;b | a==0)</code>\nin this first left <code>a&gt;b</code> will be evaluated and then <code>a==0</code> will be evaluated then <code>|</code> operation will be done</p>\n<p>but in<code>|| if a&gt;b</code> then if wont check for next RHS</p>\n<p>Similarly for &amp; and &amp;&amp;</p>\n<p><code>if(A&gt;0 &amp; B&gt;0)</code></p>\n<p>it will evalue LHS and then RHS then do bitwise &amp; but</p>\n<p>in<code>(A&gt;0 &amp;&amp; B&gt;0)</code></p>\n<p><code>if(A&gt;0)</code> is <code>false</code>(LHS) it will directly <code>return false;</code></p>\n'}]
['https://Stackoverflow.com/questions/35301', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3238/']