Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiler Error CS0019: comparing two integers

Tags:

c#

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.

like image 971
RosTam Shirani Avatar asked Jul 27 '26 04:07

RosTam Shirani


1 Answers

priority is by ==. so num & 1 == 1 should be (num & 1) == 1

See here for operator precedence.

like image 61
M.kazem Akhgary Avatar answered Jul 29 '26 17:07

M.kazem Akhgary



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!