36

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)
Community
  • 1
  • 1
Josh Mein
  • 28,107
  • 15
  • 76
  • 87

7 Answers7

32

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) )
...
dustmachine
  • 10,622
  • 5
  • 26
  • 28
  • 22
    In C# (which is a strongly typed language,) using | and & on boolean variables will result in "logical or/and w/o short-circuiting." – Mehrdad Afshari Aug 14 '09 at 17:56
  • Wow, really? That's interesting and I'll have to learn more about C#. I will update the answer to point to your comment. – dustmachine Aug 14 '09 at 18:00
  • 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. – Richard Dunlap Aug 14 '09 at 18:04
  • 9
    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 Aug 14 '09 at 18:05
  • I meant to say "...will throw a NullReferenceException if o is null..." – STW Aug 14 '09 at 18:06
  • 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. – dustmachine Aug 14 '09 at 18:42
  • @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. – Grant Wagner Aug 14 '09 at 19:21
  • 1
    @dustmachine: Actually, it does have special meaning because the operator has different behavior depending on whether the operands are integral types or **bool**: http://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**. – Grant Wagner Aug 14 '09 at 19:25
  • Sorry but that's not the answer. – user613326 Jan 25 '13 at 00:30
  • The page at https://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 – Philippe May 20 '15 at 09:04
29

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

Richard Dunlap
  • 1,957
  • 11
  • 18
8

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

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

David
  • 2,164
  • 13
  • 11
3

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

Graeme Perrow
  • 56,086
  • 21
  • 82
  • 121
1

The instance in which you're using a single character (i.e. | or &) is a bitwise comparison of the results. As long as your language evaluates these expressions to a binary value they should return the same results. As a best practice, however, you should use the logical operator as that's what you mean (I think).

Zac
  • 697
  • 3
  • 7
1

Reference:

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

Crispy
  • 5,557
  • 3
  • 30
  • 35
0

The & and | are usually bitwise operations.

Where as && and || are usually logical operations.

For comparison purposes, it's perfectly fine provided that everything returns either a 1 or a 0. Otherwise, it can return false positives. You should avoid this though to prevent hard to read bugs.

Daniel
  • 374
  • 2
  • 5