Can someone explain why is this not valid? I get "can't convert to int to bool"
if (b & 1)
also, why can't I do
b & 1
in code, is this the correct way of doing this?
int b = b & 1
if(b)
Thanks!
It's because the result of b & 1 is an integer (if b is an integer).
A correct way to do this is (among others):
if ((b & 1) != 0) { ... }
or
if (Convert.ToBoolean(b & 1)) { ... }
if can only compare boolean expressions.if (b & 1) is illegal, since b & 1 is not a boolean expression, so your question doesn't really make any sense... The expression if(b & 1) is equivalent to c = b & 1; if (c).