Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Updated Answer</strong> - my original was misleading and incomplete.</p> <p>First I should apologize for much of my comments and responses to this question.</p> <p>After reading the spec, the distinction between bitwise and conditional operators is much less clear cut.</p> <p>According to section <a href="http://www.jaggersoft.com/csharp_standard/14.10.htm" rel="noreferrer">14.10 of ECMA-334</a>:</p> <blockquote> <p>The &amp;, ^, and | operators are called the logical operators.</p> </blockquote> <p>for integer operations:</p> <blockquote> <p>1 The &amp; 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.</p> </blockquote> <p>According to section <a href="http://www.jaggersoft.com/csharp_standard/14.11.htm" rel="noreferrer">14.11:</a></p> <blockquote> <p>The &amp;&amp; and || operators are called the conditional logical operators. 2 They are also called the "short-circuiting" logical operators.</p> </blockquote> <p>14.11.1</p> <blockquote> <p>1 When the operands of &amp;&amp; or || are of type bool, or when the operands are of types that do not define an applicable operator &amp; or operator |, but do define implicit conversions to bool, the operation is processed as follows: 2 The operation x &amp;&amp; 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.</p> </blockquote> <p>14.11.2</p> <blockquote> <p>1 When the operands of &amp;&amp; or || are of types that declare an applicable user-defined operator &amp; 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 &amp;&amp; or || operation is evaluated by combining the user-defined operator true or operator false with the selected user-defined operator: 3 The operation x &amp;&amp; y is evaluated as T.false(x) ? x : T.&amp;(x, y), where T.false(x) is an invocation of the operator false declared in T, and T.&amp;(x, y) is an invocation of the selected operator &amp;. 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 &amp; 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.</p> </blockquote>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload