0x7f有什么特别之处?

编程入门 行业动态 更新时间:2024-10-25 00:35:56
本文介绍了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有什么特别之处?

本文发布于:2023-07-27 03:03:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1219964.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有什么   之处   x7f

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!