✅ **Hello, could you help me understand `&`, `&&`, `|`, `||` in C# with this code example:**
Why does the
if
condition execute when a/b>1
is false
?
The ampersand &
upon encountering the first false
, immediately triggers else
, and evaluates the rest of the expression in the background (right??). Its further calculation of other expressions after the &
should not change the outcome (?), as it is not the OR operator |
. Why do we then end up with 'everything's cool'?"14 Replies
&
is not short-circuiting.
&&
is
I'm not sure what you are asking actually, what do you mean by "evaluate the rest of the expression in the background" ?According to the information I have,
&
is an ampersand "and", returns false the first time False is triggered, but also counts the remaining expression in memory (not sure what are the use cases for it, maybe I'm wrong about it in the first place).
The double ampersand && does not read the rest of the expression the first time False (so a/b>1 gives False, the rest doesn't matter since it's already else
), thus working faster.You normally do not want to use non-short-circuiting operators for logical operations.
returns false the first time False is triggered, but also counts the remaining expression in memoryWhatever this is trying to say is not right. The entire expression gets evaluated if you use
&
and |
before producing a final result that is either true
or false
You also need to be careful to parenthesize your conditions to ensure it's doing what you think.
false & true | true
evaluates to true
false & (true | true)
evaluates to false&
and |
are called bitwise operators. They are used for manipulating binary numbers. &&
and ||
are boolean operators.Well, they can also be used as logical operators.
As is the case here.
But yes normally you never use these except for bitwise operations.
for simplicity's sake, i narrowed it down
probably stole your thunder a little bit by saying it right after your novel 😬
Nah it's fine 🙂
read this for more on binary operators: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/bitwise-and-shift-operators
read this for boolean operators: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/boolean-logical-operators
Bitwise and shift operators - perform boolean (AND, NOT, OR, XOR) a...
Learn about C# operators that perform bitwise logical (AND -
&
, NOT - ~
, OR - |
, XOR - ^
) or shift operations( <<
, and >>
) with operands of integral types.Boolean logical operators - the boolean and, or, not, and xor opera...
C# logical operators perform logical negation (
!
), conjunction (AND - &
, &&
), and inclusive and exclusive disjunction (OR - |
, ||
, ^
) operations with Boolean operands.So
false & true | true
evaluates to true whereasfalse && true | true
and false && true || true
both evaluate to falsethis whole thing is just a cluster fuck of c#
Please use your brackets though...to avoid ambiguities
Thank you so much for your help 👍
$close
Use the /close command to mark a forum thread as answered