简体   繁体   English

Java中的位移位时出现不兼容的类型错误

[英]Incompatible type error when bitshifting in Java

I'm trying to do a bitwise AND comparator on some binary objects: 我正在尝试对一些二进制对象进行按位AND比较器:

private int selectedButtons = 0x00;

private static final int ABSENCE_BUTTON_SELECTED = 0x01;
private static final int SICKNESS_BUTTON_SELECTED = 0x02;
private static final int LATENESS_BUTTON_SELECTED = 0x04;

Here is the comparator: 这是比较器:

    boolean absenceButtonEnabled = selectedButtons & ABSENCE_BUTTON_SELECTED;

But I'm getting this error: 但我收到此错误:

Error:(167, 56) error: incompatible types
required: boolean
found:    int

Any ideas? 有任何想法吗?

selectedButtons & ABSENCE_BUTTON_SELECTED is an integer, because & is the binary or operator. selectedButtons & ABSENCE_BUTTON_SELECTED是一个整数,因为&binary or运算符。

To convert it to boolean use: 要将其转换为布尔值,请使用:

boolean absenceButtonEnabled = (selectedButtons & ABSENCE_BUTTON_SELECTED) != 0;

比较它为零:

boolean absenceButtonEnabled = selectedButtons & ABSENCE_BUTTON_SELECTED != 0;

The return type of two int s is int . 两个int的返回类型是int Try the following code. 请尝试以下代码。

boolean absenceButtonEnabled = (selectedButtons & ABSENCE_BUTTON_SELECTED) == ABSENCE_BUTTON_SELECTED

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM