Java 四字节转二进制“位掩码”
我试图从二进制文件中获取四个字节,并将它们转换为一个位掩码,该位掩码代表数据的 0 和元数据的 1。
我假设我需要通过将它们或在一起来将它们转换为 int,然后通过位移来确定是否设置了位位置。
我不知道如何将所有四个组合在一起以获得可以单步执行的 32 位掩码。
int mask = (((int)hbyte) & 0xff) << 8 | (((int)lbyte) & 0xff);
这会将两个结合在一起(劳伦斯·贡萨尔维斯在另一篇文章中发布),但我无法弄清楚将四个结合在一起的语法。
或者这是枚举集的情况,我承认我并不完全理解。
任何建议将不胜感激。
谢谢。
托尼
**只是为此添加另一个快速说明,(我不确定这通常是否允许,所以如果不允许,我提前道歉),检查我新创建的 int 并检查是否有一点的最佳方法是什么设置还是不设置?
目前我正在使用 if (((comboOfBytes >> bitmaskBit) & 1) == 0) 但我不知道这是否是进行我需要的检查的最优雅的方式。
另外,我仍然不完全确定我是否理解移位实际上如何允许检查所有 32 位!
再次感谢
I'm trying to take four bytes from a binary file and turn them into a bit mask that represents a zero for data and a 1 for metadata.
I'm assuming I need to convert them into an int by or'ing them together and then bit shift through that to determine if a bit position is set or not.
I can't work out how to combine all four together to get a 32-bit mask that I can step though.
int mask = (((int)hbyte) & 0xff) << 8 | (((int)lbyte) & 0xff);
That would combine two together (post by Laurence Gonsalves from another post) but I can't figure out the syntax to get four together.
Or would this be case for an Enumset, which I'll admit I don't fully understand.
Any advice would be appreciated.
Thanks.
Tony
**Just to add another quick note to this, (I'm not sure if that's generally allowed so I apologise in advance if it's not), what is the best way of going through my newly created int and checking if a bit is set or not?
Currently I'm usingif (((comboOfBytes >> bitmaskBit) & 1) == 0)
but I don't know if that's the most elegant way of doing the check that I need.
Also I'm still not entirely sure if I understand how shifting actually allows all 32 bits to be checked!
Thanks again
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用
DataInputStream
从文件中读取 4 个字节作为 int。或者,如果读取字节 a、b、c、d:
(& 0xff) 对于防止符号位扩展破坏较高字节是必要的。
You can use a
DataInputStream
to read 4 bytes as an int from your file.Alternately, if you read bytes a, b, c, d:
The (& 0xff) is necessary to prevent sign bit extension from clobbering higher bytes.
我总是使用以下内容
但是,您可能想改用 DataStream。
不,EnumSet 不是正确的选择。 EnumSet 只是专门针对 Enums 的 Set 的高效实现。除了序列化之外,它没有 IO 功能。
I always use the following
However, you may want to use a DataStream instead.
And no, EnumSet is not the way to go. EnumSet is just an efficient implementation of Set specialized for Enums. It has no IO capabilities beyond serialization.
EnumSet 是一组 Enum 元素。您可能指的是
BitSet
An EnumSet is a Set of Enum elements. You probably mean
BitSet
我不知道你要做什么的更大背景,但如果你控制数据结构,如果你需要 32 个标志,我怀疑创建一个 boolean[32] 数组会容易得多。
是的,位标志可以节省内存,但除非您有数百万个位标志,否则这可能并不重要。
我并不是说,由一堆位标志组成的 int 然后用于屏蔽数据流从来都不是一个好主意,但我认为这很少是一个好主意。
I don't know the bigger context of what you're up to, but if you control the data structure, if you need 32 flags, I suspect it would be a whole lot easier to just create a boolean[32] array.
Yes, bit flags save memory, but unless you have millions of them, this is probably unimportant.
I'm not saying that there's never a time when an int composed of a bunch of bit flags and then being used to mask a data stream is not a good idea, but I think it is rarely a good idea.