0x7f有什么特别之处? [英] What's so special about 0x7f?

查看:107
本文介绍了0x7f有什么特别之处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读avro格式规范,并试图了解其实现.这是用于解码长值的方法:

I'm reading avro format specification and trying to understand its implementation. Here is the method for decoding long value:

  @Override
  public long readLong() throws IOException {
    ensureBounds(10);
    int b = buf[pos++] & 0xff;
    int n = b & 0x7f;
    long l;
    if (b > 0x7f) {
      b = buf[pos++] & 0xff;
      n ^= (b & 0x7f) << 7;
      if (b > 0x7f) {
        b = buf[pos++] & 0xff;
        n ^= (b & 0x7f) << 14;
        if (b > 0x7f) {
          b = buf[pos++] & 0xff;
          n ^= (b & 0x7f) << 21;
          if (b > 0x7f) {
            // only the low 28 bits can be set, so this won't carry
            // the sign bit to the long
            l = innerLongDecode((long)n);
          } else {
            l = n;
          }
        } else {
          l = n;
        }
      } else {
        l = n;
      }
    } else {
      l = n;
    }
    if (pos > limit) {
      throw new EOFException();
    }
    return (l >>> 1) ^ -(l & 1); // back to two's-complement
  }

问题是为什么我们总是检查 0x7f 是否少于我们刚刚读取的字节?

The question is why do we always check if 0x7f less then the byte we just read?

推荐答案

这是位打包的一种形式,其中每个 byte 的最高有效位用于确定是否另外一个 byte应该被阅读.本质上,这使您可以用比通常所需更少的字节数来编码值.但是,需要注意的是,如果数量很大,那么将需要的字节数超过了 normal 个字节.因此,这在处理小值时是成功的.

This is a form of bit-packing where the most significant bit of each byte is used to determine if another byte should be read. Essentially, this allows you to encode values in a fewer amount of bytes than they would normally require. However, there is the caveat that, if the number is large, then more than the normal amount of bytes will be required. Therefore, this is successful when working with small values.

关于您的问题,二进制代码 0111_1111 0x7F .您会看到最高有效位被用作标志位.

Getting to your question, 0x7F is 0111_1111 in binary. You can see that the most significant bit is used as the flag bit.

这篇关于0x7f有什么特别之处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆