If I have the following C# code:
int i = 1;
bool b = true;
if (i & 1 == 1 && b)
...
I get an error:
operator & cannot be applied to operands of type int and bool
I can do:
if (i & 1 == 1)
and
if (b)
I have tried putting them in brackets:
if ((i & 1 == 1) && b)
if (i & 1 == 1 && (b))
if ((i & 1 == 1) && (b))
but it made no difference.
What am I missing here?
I know I can just put them in separate ifs, but I'm trying to understand what's going on here. Is it a bug?
I'm using VS 2015. Target framework is 4.6.2.
Precedence rules; ==
is higher than &
or &&
; so in the left expression (i & 1 == 1
) you actually mean: (i & 1) == 1
. Then the combined expression is:
if ((i & 1) == 1 && b)
Actually, it is odd that you say this works:
if (i & 1 == 1)
because I get CS0019 "Operator '&' cannot be applied to operands of type 'int' and 'bool'" for that!
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments