C
C#4mo ago
flask

✅ **Hello, could you help me understand `&`, `&&`, `|`, `||` in C# with this code example:**

int a = 4, b = 3, c = 2, d = 1;

if (a / b > 1 & c - d < 1 | a % 2 == 0)
label1.Text = "everything's cool";
else
label1.Text = "everything's bad";
int a = 4, b = 3, c = 2, d = 1;

if (a / b > 1 & c - d < 1 | a % 2 == 0)
label1.Text = "everything's cool";
else
label1.Text = "everything's bad";
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
mtreit
mtreit4mo ago
& 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" ?
flask
flask4mo ago
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.
mtreit
mtreit4mo ago
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 memory
Whatever 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
oke
oke4mo ago
& and | are called bitwise operators. They are used for manipulating binary numbers. && and || are boolean operators.
mtreit
mtreit4mo ago
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.
oke
oke4mo ago
for simplicity's sake, i narrowed it down probably stole your thunder a little bit by saying it right after your novel 😬
mtreit
mtreit4mo ago
Nah it's fine 🙂
oke
oke4mo ago
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.
mtreit
mtreit4mo ago
So false & true | true evaluates to true whereasfalse && true | true and false && true || true both evaluate to false
oke
oke4mo ago
this whole thing is just a cluster fuck of c#
Moods
Moods4mo ago
Please use your brackets though...to avoid ambiguities
flask
flask4mo ago
Thank you so much for your help 👍
Denis
Denis3mo ago
$close
MODiX
MODiX3mo ago
Use the /close command to mark a forum thread as answered