在 Java 中进行位移时出现类型不兼容错误
Incompatible type error when bitshifting in Java
我正在尝试对某些二进制对象执行按位与比较:
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;
这是比较器:
boolean absenceButtonEnabled = selectedButtons & ABSENCE_BUTTON_SELECTED;
但是我收到了这个错误:
Error:(167, 56) error: incompatible types
required: boolean
found: int
有什么想法吗?
将其与零进行比较:
boolean absenceButtonEnabled = selectedButtons & ABSENCE_BUTTON_SELECTED != 0;
selectedButtons & ABSENCE_BUTTON_SELECTED
是一个整数,因为 &
是 binary or
运算符。
要将其转换为布尔值,请使用:
boolean absenceButtonEnabled = (selectedButtons & ABSENCE_BUTTON_SELECTED) != 0;
两个int
的return类型是int
。试试下面的代码。
boolean absenceButtonEnabled = (selectedButtons & ABSENCE_BUTTON_SELECTED) == ABSENCE_BUTTON_SELECTED
我正在尝试对某些二进制对象执行按位与比较:
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;
这是比较器:
boolean absenceButtonEnabled = selectedButtons & ABSENCE_BUTTON_SELECTED;
但是我收到了这个错误:
Error:(167, 56) error: incompatible types
required: boolean
found: int
有什么想法吗?
将其与零进行比较:
boolean absenceButtonEnabled = selectedButtons & ABSENCE_BUTTON_SELECTED != 0;
selectedButtons & ABSENCE_BUTTON_SELECTED
是一个整数,因为 &
是 binary or
运算符。
要将其转换为布尔值,请使用:
boolean absenceButtonEnabled = (selectedButtons & ABSENCE_BUTTON_SELECTED) != 0;
两个int
的return类型是int
。试试下面的代码。
boolean absenceButtonEnabled = (selectedButtons & ABSENCE_BUTTON_SELECTED) == ABSENCE_BUTTON_SELECTED