我有四种类型的扇区,每种扇区都由一个二进制字节表示:
sector one: 0000 0001
sector two: 0000 0010
sector three: 0000 0100
sector four: 0000 1000
现在,我得到一个返回整数值的函数,我需要检查该值是否是扇区之一。最有效的检查方法是什么?我的意思是按位运算。
int val = func();
//check if val is one of the sector
最佳答案
boolean isSectorOne (int val) {return (val & 0x01) == 0x01;}
boolean isSectorTwo (int val) {return (val & 0x02) == 0x02;}
boolean isSectorThree (int val) {return (val & 0x04) == 0x04;}
boolean isSectorFour (int val) {return (val & 0x08) == 0x08;}
关于java - 按位运算来比较值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28831028/