Difference between | and || or & and && for comparison

c#, comparison, operators

Solution

in C (and other languages probably) a single `|` or `&` is a bitwise comparison. The double `||` or `&&` is a logical comparison. Edit: Be sure to read Mehrdad's comment below regarding "without short-circuiting"

In practice, since `true` is often equivalent to `1` and `false` is often equivalent to `0`, the bitwise comparisons can sometimes be valid and return exactly the same result.

There was once a mission critical software component I ran a static code analyzer on and it pointed out that a bitwise comparison was being used where a logical comparison should have been. Since it was written in C and due to the arrangement of logical comparisons, the software worked just fine with either. Example:

if ( (altitide > 10000) & (knots > 100) )
...

Problem

Possible Duplicate: A clear, layman’s explanation of the difference between | and || in c# ? What is the difference between comparing with | and || or & and && in C# and Javascript? Examples: ``` if(test == test1 | test1 == test2) or if(test == test1 || test1 == test2) if(test == test1 & test1 == test2) or if(test == test1 && test1 == test2) ```

Original source

Related problems