大端字节序的位掩码
这不是一个问题,而是一个健全性检查!
如果您需要将 4 个字节作为 Big endian 中的位掩码读入 Java,这些字节为:
0x00、0x01、0xB6、0x02。
将其转换为 int 将为: 112130
二进制为: 00000000000000011010011000000010
一系列字节的字节序不会影响位位置,不是吗?
谢谢托尼
This isn't a question as much as it's a sanity check!
If you needed to read 4 bytes into Java as a bitmask in Big endian and those bytes were:
0x00, 0x01, 0xB6, 0x02.
Making that into an int would be: 112130
The binary would be: 00000000000000011010011000000010
The endian of a series of bytes wouldn't affect the bit position, would it?
Thanks
Tony
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
字节顺序反映了字节的顺序,但不反映这些字节内位的顺序。
假设我想表示(两字节)字 0x9001。
如果我只是以二进制形式输入,则为 1001000000000001。
如果我将字节(从低地址到高地址)转储到大端机器上,我会看到
10010000 00000001
。如果我在小端机器上转储字节(从低地址到高地址),我会看到
00000001 10010000
。Endian-ness reflects the ordering of bytes, but not the ordering of the bits within those bytes.
Let's say I want to represent the (two-byte) word 0x9001.
If I just type this out in binary, that would be 1001000000000001.
If I dump the bytes (from lower address to higher) on a big-endian machine, I would see
10010000 00000001
.If I dump the bytes (from lower address to higher) on a little-endian machine, I would see
00000001 10010000
.一般来说,如果您正在读取的内容为您提供了整个字节,那么您无需担心组成这些字节的位的顺序:正如您正确假设的那样,重要的是字节的顺序。
您可能需要担心各个位的“字节顺序”的时间是您实际读取/写入位流而不是整个字节的时候(例如,如果您正在编写在位级别运行的压缩算法,那么您' d 必须决定以什么顺序写入位)。
In general, if the thing you're reading from is giving you whole bytes, then you don't need to worry about the order of bits making up those bytes: it is just the order of the bytes that matters, as you correctly suppose.
The time you might have to worry about the "endianness" of individual bits is where you're actually reading/writing a stream of bits rather than whole bytes (e.g. if you were writing a compression algorithm that operated at the bit level, you'd have to make a decision about what order to write the bits in).
您唯一需要注意的是如何准确地“将 4 个字节读入 Java”——这就是字节序很重要的地方,您可能会搞砸它(
DataInputStream
假定为大字节序)。一旦你读到的值变成了 int 112130,你就设置好了。The only thing you have to pay attention is how exactly you "read 4 bytes into Java" - that's where endianness matters and you can mess it up (
DataInputStream
assumes big endian). Once the value you've read has become the int 112130, you're set.