[英]Operator “&” cannot be applied to byte, int, boolean
Upon my previous question about how to compare if combined bits contain a specific bit I am running into this error. 根据我之前关于如何比较组合位是否包含特定位的问题,我遇到了这个错误。
int flag1 = 1 << 0;
int flag4 = 1 << 5;
int combined = flag1 | flag4;
if (combined & flag1 == flag1) // <-- Operator & cannot be applied to int, boolean
If I cast the flags to byte the error replaces int
with byte
. 如果我将标志转换为byte,则错误将
int
替换为byte
。
The compiler sees the binary operator &
in your if
statement, treats it as logical AND (since it expects an expression that returns a boolean
), and checks the types of the arguments. 编译器在
if
语句中看到二元运算符&
,将其视为逻辑AND(因为它需要一个返回boolean
的表达式),并检查参数的类型。
It encounters one int
argument - combined
- and one boolean
argument - flag1 == flag1
. 它遇到一个
int
参数 - combined
- 和一个boolean
参数 - flag1 == flag1
。 Since it expects two boolean
arguments (the &
operator cannot be applied to an int
and a boolean
), it gives an error. 由于它需要两个
boolean
参数( &
运算符不能应用于int
和boolean
),因此会出错。
Add parentheses in order for the operators to be evaluated in the desired order : 添加括号以便按所需顺序评估运算符:
if ((combined & flag1 ) == flag1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.