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

129,458

Solution 1

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

Solution 2

& and | are bitwise operators that can operate on both integer and Boolean arguments, and && and || are logical operators that can operate only on Boolean arguments. In many languages, if both arguments are Boolean, the key difference is that the logical operators will perform short circuit evaluation and not evaluate the second argument if the first argument is enough to determine the answer (e.g. in the case of &&, if the first argument is false, the second argument is irrelevant).

Solution 3

& and | are binary operators while || and && are boolean.

The big difference:
(1 & 2) is 0, false
(1 && 2) is true

Solution 4

(Assuming C, C++, Java, JavaScript)

| and & are bitwise operators while || and && are logical operators. Usually you'd want to use || and && for if statements and loops and such (i.e. for your examples above). The bitwise operators are for setting and checking bits within bitmasks.

Solution 5

Reference:

http://en.wikipedia.org/wiki/Short-circuit_evaluation

Share:
129,458
Josh Mein
Author by

Josh Mein

Main areas of expertise: SQL C# Javascript & jQuery ASP.Net WebForms ASP.Net MVC

Updated on May 27, 2020

Comments

  • Josh Mein
    Josh Mein almost 4 years

    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)
    
  • mmx
    mmx over 14 years
    In C# (which is a strongly typed language,) using | and & on boolean variables will result in "logical or/and w/o short-circuiting."
  • dustmachine
    dustmachine over 14 years
    Wow, really? That's interesting and I'll have to learn more about C#. I will update the answer to point to your comment.
  • Chris Zwiryk
    Chris Zwiryk over 14 years
    (1 && 2) is not valid C# syntax.
  • David
    David over 14 years
    upvote for being first to mention the short circuiting of boolean operators
  • David
    David over 14 years
    The original question didn't mention C#. It was edited after.
  • Richard Dunlap
    Richard Dunlap over 14 years
    Mehrdad's comment is actually not unique to C# -- many (most?) strongly typed languages have this distinction. I know it's present in Java and C++, and I assume it's present in C, for example.
  • STW
    STW over 14 years
    An example of C# behaviors is "if ((o != null) & (o.Property == 1))" will throw a NullReferenceException since it will still try to evaluate the value of o.Property even if o is null. "if ((o != null) && (o.Property == 1))" won't throw the exception, since it won't try to evaluate o.Property if o == null.
  • STW
    STW over 14 years
    I meant to say "...will throw a NullReferenceException if o is null..."
  • dustmachine
    dustmachine over 14 years
    Ah, this makes sense of course. Since & means we're trying to perform a bitwise "AND" operation, we have to evaluate everything or there's nothing to perform the bitwise "AND" upon. And the light bulb goes on. So it's not that it has special meaning such as "perform no short-circuiting", it's that it has to perform all conditionals in order complete the task of the bitwise operation.
  • Pyro
    Pyro over 14 years
    @Yoooder: I hope no one is actually using & and | to do logical and/or w/o short-circuiting in production code. The only reason I can think that someone would use them would be to ensure side-effects of the expression(s) always happen. That just seems... very bad.
  • Pyro
    Pyro over 14 years
    @dustmachine: Actually, it does have special meaning because the operator has different behavior depending on whether the operands are integral types or bool: msdn.microsoft.com/en-us/library/sbf85k1c.aspx : Binary & operators are predefined for the integral types and bool. For integral types, & computes the bitwise AND of its operands. For bool operands, & computes the logical AND of its operands; that is, the result is true if and only if both its operands are true.
  • user613326
    user613326 about 11 years
    Sorry but that's not the answer.
  • Philippe
    Philippe almost 9 years
    The page at msdn.microsoft.com/en-us/library/sbf85k1c.aspx states that "The & operator evaluates both operators regardless of the first one's value.", which seems to encompass the boolean predefined ones. However, the C# language specifications do not make such a guarantee in section 7.11.3