Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用位掩码检查字节流中的字节_Java_Binary_Bitmask - Fatal编程技术网

Java 如何使用位掩码检查字节流中的字节

Java 如何使用位掩码检查字节流中的字节,java,binary,bitmask,Java,Binary,Bitmask,我有bytebuffer,每次迭代我都要读取8个字节(长),我有我想要比较的位掩码,但我不确定我的模式应该是什么样子。比如说, private final static long ASCII_125 = 0x7D; public static boolean isValidPath( DirectBuffer path) { boolean isPathValid = true; for (int i = 0; i < path.capacity(); i +=

我有
bytebuffer
,每次迭代我都要读取8个字节(长),我有我想要比较的位掩码,但我不确定我的模式应该是什么样子。比如说,

private final static long ASCII_125 = 0x7D;

public static boolean isValidPath(
        DirectBuffer path)
{
  boolean isPathValid = true;
  for (int i = 0; i < path.capacity(); i += Long.BYTES)
  {
      long charsLong = path.getLong(i);
      if ((charsLong & result) != xxxxxx(pattern))
      {
          isPathValid = false;
          break;
      }
  }
  return isPathValid;
}
private final静态长ASCII_125=0x7D;
公共静态布尔值isValidPath(
DirectBuffer路径)
{
布尔值isPathValid=true;
for(int i=0;i

因此,如果charlong是
0b01111011\U 011111011\U 00100101\U 011111011\U 011111011\U 011111101\U 011111101
并检查这8个字节中是否存在
011111101
字节。我想我需要找到逻辑上允许我一次检查多个值的模式,为该模式构造一个掩码,然后一次将掩码应用于所有8个字节。但是我很难想出这个模式。

像这样的东西怎么样:

这基本上是通过将长的右8位移位并将其掩蔽到每个字节来检查每个字节 一个字节


谢谢@WJS让我看一看仅供参考。我将其转换为long的原因是,我希望每次迭代检查8个字节,而不是每次检查1个字节。
  for (int i = 0; i < path.capacity(); i += Long.BYTES) { 
      long charsLong = path.getLong(i);
      while(charsLong > 0) {
          // get low order byte
          byte val = (byte)(charLong&0xFF);
          if ((val & result) != xxxxxx(pattern)) {
             return false;
          }
          // right shift thru sign bit by 8 bits 
          charsLong>>>= 8; 
      }
   }
   return true;
byte bitMask = 0b101;

if ((test & bitMask) == bitMask) {
   bits are set.
}