In C#, why does the first if statement gives me a compile error but not the second one? Can someone please explain?
1) if(num & 1 == 1) { Console.WriteLine("Test"); }
This gives me: Error CS0019 Operator '&' cannot be applied to operands of type 'int' and 'bool'.
2)
var x = num & 1;
if (x == 1) { Console.WriteLine("Test"); }
This compiles perfectly.
priority is by ==. so num & 1 == 1 should be (num & 1) == 1
See here for operator precedence.
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