Java的迭代位的字节数组 [英] Java Iterate Bits in Byte Array

查看:148
本文介绍了Java的迭代位的字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何迭代比特字节数组?

How can i iterate bits in a byte array?

推荐答案

您不得不写自己的实施可迭代<布尔> 历时字节数组,然后创建的Iterator<布尔>后者想起了当前索引入字节数组的的当前字节中的当前索引。然后,像这样的一个工具方法会派上用场:

You'd have to write your own implementation of Iterable<Boolean> which took an array of bytes, and then created Iterator<Boolean> values which remembered the current index into the byte array and the current index within the current byte. Then a utility method like this would come in handy:

private static Boolean isBitSet(byte b, int bit)
{
    return (b & (1 << bit)) != 0;
}

(其中的取值范围为0〜7)。每次的next()叫你不得不当前字节内增加你的位索引,并增加字节数组中的字节索引,如果你达到了第9位

(where bit ranges from 0 to 7). Each time next() was called you'd have to increment your bit index within the current byte, and increment the byte index within byte array if you reached "the 9th bit".

这不是一个真正的硬盘的 - 但有点痛。让我知道,如果你想一个样本实现...

It's not really hard - but a bit of a pain. Let me know if you'd like a sample implementation...

这篇关于Java的迭代位的字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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