Ok, so I've read about this a number of times, but I'm yet to hear a clear, easy to understand (and memorable) way to learn the difference between:
if (x | y)
and
if (x || y)
..within the context of C#. Can anyone please help me learn this basic truth, and how C# specifically, treats them differently (because they seem to do the same thing). If the difference a given piece of code has between them is irrelevant, which should I default to as a best-practise?
The | operator evaluates both operands even if the left-hand operand evaluates to true, so that the operation result is true regardless of the value of the right-hand operand. The conditional logical OR operator ||, also known as the "short−circuiting" logical OR operator, computes the logical OR of its operands.
OR ( || ) - If EITHER or BOTH sides of the operator is true, the result will be true. AND ( && ) - If BOTH and ONLY BOTH sides of the operator are true, the result will be true.
Differences between | and || operators in Java | is a bitwise operator and compares each operands bitwise. It is a binary OR Operator and copies a bit to the result it exists in either operands. (A | B) will give 61 which is 0011 1101. Whereas || is a logical OR operator and operates on boolean operands.
&& is used to perform and operation means if anyone of the expression/condition evaluates to false whole thing is false. || is used to perform or operation if anyone of the expression/condition evaluates to true whole thing becomes true.
||
is the logical-or operator. See here. It evaluates to true
if at least one of the operands is true. You can only use it with boolean operands; it is an error to use it with integer operands.
// Example var one = true || bar(); // result is true; bar() is never called var two = true | bar(); // result is true; bar() is always called
|
is the or operator. See here. If applied to boolean types, it evaluates to true
if at least one of the operands is true. If applied to integer types, it evaluates to another number. This number has each of its bits set to 1 if at least one of the operands has a corresponding bit set.
// Example var a = 0x10; var b = 0x01; var c = a | b; // 0x11 == 17 var d = a || b; // Compile error; can't apply || to integers var e = 0x11 == c; // True
For boolean operands, a || b
is identical to a | b
, with the single exception that b
is not evaluated if a
is true. For this reason, ||
is said to be "short-circuiting".
If the difference a given piece of code has between them is irrelevant, which should I default to as a best-practise?
As noted, the difference isn't irrelevant, so this question is partially moot. As for a "best practice", there isn't one: you simply use whichever operator is the correct one to use. In general, people favor ||
over |
for boolean operands since you can be sure it won't produce unnecessary side effects.
When used with boolean operands the |
operator is a logical operator just as ||
, but the difference is that the ||
operator does short circuit evaluation and the |
operator does not.
This means that the second operand is always evaluated using the |
operator, but using the ||
operator the second operand is only evaluated if the first operand evaluates to false.
The result of the expression is always the same for both operatrors, but if the evaluation of the second operand causes something else to change, that is only guaranteed to happen if you use the |
operator.
Example:
int a = 0; int b = 0; bool x = (a == 0 || ++b != 0); // here b is still 0, as the "++b != 0" operand was not evaluated bool y = (a == 0 | ++b != 0); // here b is 1, as the "++b != 0" operand was evaluated.
The short-circuit evaluation of the ||
operator can be used to write shorter code, as the second operand only is evaluated if the first operand is true. Instead of writing like this:
if (str == null) { Console.WriteLine("String has to be at least three characters."); } else { if (str.Length < 3) { Console.WriteLine("String has to be at least three characters."); } else{ Console.WriteLine(str); } }
You can write like this:
if (str == null || str.Length < 3) { Console.WriteLine("String has to be at least three characters."); } else{ Console.WriteLine(str); }
The second operand is only evaluated if the first is false, so you know that you can safely use the string reference in the second operand as it can not be null if the second operand is evaluated.
In most cases you would want to use the ||
operator rather than the |
operator. If the first operand is false, there is no need to evaluate the second operand to get the result. Also, a lot of people (evidently) doesn't know that you can use the |
operator with boolean operands, so they would get confused when seeing it used that way in the code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With