如何检查给定位中的单个位是1还是0?



如何检查位数中的数字是1还是0 ?例如:给定编号:0b00001101位索引0的位置= true位索引1的位置= false

我在确定位的位置时卡住了。

检查数字的二进制表示的给定位置(例如n)是否为0的一种方法是将数字n移位并检查移位的最后一位数字。在示例

public class Main {
public static void main(String[] args) {
System.out.println(checkPosition(Byte.parseByte(String.valueOf(3)),1));
}
// Return true if the given position is 0, false otherwise
public static boolean checkPosition(Byte b, int position)    {
return (b.byteValue() >> position) % 2 == 0;
}
}
// Output: false

最新更新