大端字节序的位掩码

发布于 2024-10-12 21:47:11 字数 238 浏览 4 评论 0原文

这不是一个问题,而是一个健全性检查!

如果您需要将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

橘虞初梦 2024-10-19 21:47:11

字节顺序反映了字节的顺序,但不反映这些字节内位的顺序。

假设我想表示(两字节)字 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.

短暂陪伴 2024-10-19 21:47:11

一般来说,如果您正在读取的内容为您提供了整个字节,那么您无需担心组成这些字节的位的顺序:正如您正确假设的那样,重要的是字节的顺序。

您可能需要担心各个位的“字节顺序”的时间是您实际读取/写入位流而不是整个字节的时候(例如,如果您正在编写在位级别运行的压缩算法,那么您' 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).

一袭白衣梦中忆 2024-10-19 21:47:11

您唯一需要注意的是如何准确地“将 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文