operator & cannot be applied to operands of type int and bool when using bitwise and

Graham

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.

Marc Gravell

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.

edited at
0

Comments

0 comments
Login to comment

Related

Operator && cannot be applied to operands of type 'int' and 'bool'

Operator '||' cannot be applied to operands of type 'bool?' and 'bool?'

Operator && cannot be applied to operands of type double and bool

operator '||' cannot be applied to operands of type string and bool

Operator '&&' cannot be applied to operands of type 'bool' and 'string' when using if else statement in C# for unity

Operator '!=' cannot be applied to operands of type 'Task' and 'int'

Operator '/' cannot be applied to operands of type 'int' and 'Task'

Operator '&&' cannot be applied to operands of type 'Task<bool>' and 'bool'

How to rectify the error Operator '&&' cannot be applied to operands of type 'bool?' and 'bool'

Why I get error message Operator '||' cannot be applied to operands of type 'bool' and 'int'

Operator '&&' can't be applied to operands of type 'int' and 'bool'

Operator '||' cannot be applied to operands of type 'bool' and 'lambda expression'

Swift 2: Binary operator '==' cannot be applied to operands of type '()?' and 'Bool'

Operator '*' cannot be applied to operands of type 'Vector3' and 'bool'

Operator '&&' cannot be applied operands to bool and bool?

Binary operator '..<' cannot be applied to operands of type 'Int' and 'Int?'

Binary operator cannot be applied to operands of type int and int? Swift 3

Binary operator '==' cannot be applied to operands of type '(Any) -> Int' and 'Int'

Error : Binary operator '-=' cannot be applied to operands of type 'Int?' and 'Int'

Operator '==' cannot be applied to operands of type

Operator + cannot be applied to operands of type IntPtr and int - .Net 3.5

Binary operator * cannot be applied to operands of type Int and CGFloat

Binary operator '|' cannot be applied to operands of type 'Int' and 'UInt8'

Binary operator '/' cannot be applied to operands of type 'UIColor' and 'Int'

Binary operator '/' cannot be applied to operands of type 'CGSize' and 'Int'

Binary operator '==' cannot be applied to operands of type 'Any?' and 'Int'

Binary Operator + Cannot be Applied to Operands of Type CGfloat int

Binary operator '+' cannot be applied to operands of type 'DispatchTime' and 'Int32'

Operator '>=' cannot be applied to operands of type 'object' and 'int' in datagridview cell values