Difference between eager operation and short-circuit operation? (| versus || and & versus &&)

Solution 1:

& can be used in two different ways: bitwise "and" and logical "and"

The difference between logical & and && is only that in case you use &, the second expression is also evaluated, even if the first expression was already false. This may (for example) be interesting if you want to initialize two variables in the loop:

if ((first = (i == 7)) & (second = (j == 10))) { //do something }

if you use this syntax, first and second will always have a value, if you use

if ((first = (i == 7)) && (second = (j == 10))) { //do something }

it may be that only first has a value after the evaluation.

It is the same for | and ||: In case you use |, both of the expressions are always evaluated, if you use || it may be that only the first expression is evaluated, which would be the case if the first expression is true.

In contrast, in other applications && can be the better choice. If myNumber is of type int?, you could have something like

if (myNumber != null && myNumber.Value == 7)

and this would only evaluate myNumber != null at first, and it would only evaluate the second expression, if the null check was okay.

if (myNumber != null & myNumber.Value == 7)

would finish with a NullPointerException during the evaluation of the second expression, if myNumber was null. Therefore, you would use && in this context.

Solution 2:

you should read this Short-circuit evaluation.

Solution 3:

&& and || are used with booleans. Your output makes sense when using these.

& and | are bitwise operator, meaning they are applied to the operands bit by bit. For example

110010010 |
001000100 =
111010110

using the same table of your program's output but a bit a time. They are mainly used with integers, not booleans.

Solution 4:

The difference will be less apparent in booleans; bitwise operators are primarily for numbers, whereas logical operators are primarily for booleans. In a bitwise operation (e.g., &), the operation is performed for each bit. In a logical operation (e.g., &&), the operation is performed for the entire result.

For example, the bitwise & of 11 (1011 in binary) and 2 (10 in binary) would be computed as such:

  1011
& 0010
______
  0010

which is 2.

There is an additional consideration in how the two types of operators are executed. When using a bitwise operator, the expressions on either side of the operator are first executed, and then the operation is performed. When using a logical operator, however, the expression on the left side of the operator is performed first, and the right side may be neglected if it will not change the result.

For example, when I execute (false expression) && (true expression), the true expression is never evaluated. Likewise with (true expression) || (false expression). This is referred to as short-circuit evaluation