匹配字节模式



假设您有一个模式字节:

byte b = 0x%1;

如何判断一个字节何时在"第二个位置"(代替%)上有特定值?在这个例子1中,无论第一个位置是什么。

使用掩码位获取最后8位:

int last8bits = b & 0xF;

编辑:您应该了解按位操作。


完整示例:

public static void main(String[] args) {
    byte b = (byte) 0xA1;
    int last8bits = b & 0xF;
    if (last8bits == 0x01)
        System.out.println(""matches"");
}
if ((0x0F & b) == 0x01) {
    // pattern matched

相关内容

最新更新