简体   繁体   English

运算符“&”不能应用于byte,int,boolean

[英]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参数( &运算符不能应用于intboolean ),因此会出错。

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.

相关问题 运算符 &amp;&amp; 不能应用于 &#39;boolean&#39;, &#39;int - Operator && cannot be applied to 'boolean', 'int 错误消息“运算符'&&'无法应用于'int''boolean' - Error message "Operator '&&' cannot be applied to 'int' 'boolean' 错误信息:运算符 &lt; 不能应用于 boolean,int - Error message: operator < cannot be applied to boolean,int 操作员! 不能应用于int - Operator ! cannot be applied to int 出现“运算符‘&&’不能应用于‘布尔值’、‘int’”错误,我不确定为什么 - Getting an "Operator '&&' cannot be applied to 'boolean', 'int'" error and I'm unsure why 运算符“||” 不能应用于 'boolean'、'void' [暂停] - Operator '||' cannot be applied to 'boolean', 'void' [on hold] “运算符 &#39;&lt;&#39; 不能应用于 &#39;<lambda parameter> &#39;,&#39;int&#39;”在java中 - "Operator '<' cannot be applied to '<lambda parameter>', 'int' " in java 如何按位操作? (运算符&不能应用于Integer或int) - How to operate bitwise? (Operator & cannot be applied to Integer or int) 运算符“+”不能应用于“int”、“java.lang.Object” - Operator '+' cannot be applied to 'int', 'java.lang.Object' 运算符“ &lt;”不能应用于android.widget.textview int - operator '<' cannot be applied to android.widget.textview int
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM