What is the difference between logical and conditional AND, OR in C#?

C#Conditional OperatorLogical Operators

C# Problem Overview


> Possible Duplicate:
> What is the diffference between the | and || or operators?

Logical AND and OR:

(x & y)
(x | y)

Conditional AND and OR:

(x && y)
(x || y)

I've only known about conditional operands up to this point. I know what it does and how to apply it in if-statements. But what is the purpose of logical operands?

C# Solutions


Solution 1 - C#

I prefer to think of it as "bitwise vs. conditional" rather than "logical vs conditional" since the general concept of "logical" applies in both cases.

x & y    // bitwise AND, 0101 & 0011 = 0001
x | y    // bitwise OR,  0101 | 0011 = 0111

x && y   // true if both x and y are true
x || y   // true if either x or y are true

Edit

By popular demand, I should also mention that the arguments are evaluated differently. In the conditional version, if the result of the entire operation can be determined by the first argument, the second argument is not evaluated. This is called short-circuit evaluation. Bitwise operations have to evaluate both sides in order to compute the final value.

For example:

x.foo() && y.bar()

This will only call y.bar() if x.foo() evaluates to true. Conversely,

x.foo() || y.bar()

will only call y.bar() if x.foo() evaluates to false.

Solution 2 - C#

(x && y) 

is lazy. It will only evaluate y if x is true.

(x & y)

is not lazy. y will always be evaluated.

Solution 3 - C#

Updated Answer - my original was misleading and incomplete.

First I should apologize for much of my comments and responses to this question.

After reading the spec, the distinction between bitwise and conditional operators is much less clear cut.

According to section 14.10 of ECMA-334:

> The &, ^, and | operators are called > the logical operators.

for integer operations:

> 1 The & operator computes the bitwise > logical AND of the two operands, the | > operator computes the bitwise logical > OR of the two operands, and the ^ > operator computes the bitwise logical > exclusive OR of the two operands. 2 No > overflows are possible from these > operations.

According to section 14.11:

> The && and || operators are called the > conditional logical operators. 2 They > are also called the "short-circuiting" > logical operators. >

14.11.1

> 1 When the operands of && or || are of > type bool, or when the operands are of > types that do not define an applicable > operator & or operator |, but do > define implicit conversions to bool, > the operation is processed as follows: > 2 The operation x && y is evaluated as > x ? y : false. 3 In other words, x is > first evaluated and converted to type > bool. 4 Then, if x is true, y is > evaluated and converted to type bool, > and this becomes the result of the > operation. 5 Otherwise, the result of > the operation is false. 6 The > operation x || y is evaluated as x ? > true : y. 7 In other words, x is first > evaluated and converted to type bool. > 8 Then, if x is true, the result of > the operation is true. 9 Otherwise, y > is evaluated and converted to type > bool, and this becomes the result of > the operation. >

14.11.2

> > 1 When the operands of && or || are of > types that declare an applicable > user-defined operator & or operator |, > both of the following must be true, > where T is the type in which the > selected operator is declared: 2 The > return type and the type of each > parameter of the selected operator > must be T. 3 In other words, the > operator must compute the logical AND > or the logical OR of two operands of > type T, and must return a result of > type T. 4 T must contain declarations > of operator true and operator false. > Paragraph 2 1 A compile-time error > occurs if either of these requirements > is not satisfied. 2 Otherwise, the && > or || operation is evaluated by > combining the user-defined operator > true or operator false with the > selected user-defined operator: 3 The > operation x && y is evaluated as > T.false(x) ? x : T.&(x, y), where > T.false(x) is an invocation of the > operator false declared in T, and > T.&(x, y) is an invocation of the > selected operator &. 4 In other words, > x is first evaluated and operator > false is invoked on the result to > determine if x is definitely false. 5 > Then, if x is definitely false, the > result of the operation is the value > previously computed for x. 6 > Otherwise, y is evaluated, and the > selected operator & is invoked on the > value previously computed for x and > the value computed for y to produce > the result of the operation. 7 The > operation x || y is evaluated as > T.true(x) ? x : T.|(x, y), where > T.true(x) is an invocation of the > operator true declared in T, and > T.|(x, y) is an invocation of the > selected operator |. 8 In other words, > x is first evaluated and operator true > is invoked on the result to determine > if x is definitely true. 9 Then, if x > is definitely true, the result of the > operation is the value previously > computed for x. 10 Otherwise, y is > evaluated, and the selected operator | > is invoked on the value previously > computed for x and the value computed > for y to produce the result of the > operation. Paragraph 3 1 In either of > these operations, the expression given > by x is only evaluated once, and the > expression given by y is either not > evaluated or evaluated exactly once. > Paragraph 4 1 For an example of a type > that implements operator true and > operator false, see §18.4.2.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSahat YalkabovView Question on Stackoverflow
Solution 1 - C#CogwheelView Answer on Stackoverflow
Solution 2 - C#Robert HarveyView Answer on Stackoverflow
Solution 3 - C#John WeldonView Answer on Stackoverflow