Java 迭代字节数组中的位
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1034473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Iterate Bits in Byte Array
提问by Hamza Yerlikaya
How can i iterate bits in a byte array?
如何迭代字节数组中的位?
采纳答案by Jon Skeet
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 andthe current index within the current byte. Then a utility method like this would come in handy:
你必须写自己的实施Iterable<Boolean>历时字节数组,然后创造Iterator<Boolean>价值,其记忆中的当前索引的字节数组和当前字节中的当前索引。然后像这样的实用方法会派上用场:
private static Boolean isBitSet(byte b, int bit)
{
return (b & (1 << bit)) != 0;
}
(where bitranges 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".
(bit范围从 0 到 7)。每次next()调用时,您都必须在当前字节内增加位索引,如果达到“第 9 位”,则在字节数组中增加字节索引。
It's not really hard- but a bit of a pain. Let me know if you'd like a sample implementation...
这并不难- 但有点痛苦。如果您想要一个示例实现,请告诉我...
回答by HasaniH
You can iterate through the byte array, and for each byte use the bitwise operators to iterate though its bits.
您可以遍历字节数组,并为每个字节使用按位运算符来遍历其位。
回答by Paul Sonier
Original:
原来的:
for (int i = 0; i < byteArray.Length; i++)
{
byte b = byteArray[i];
byte mask = 0x01;
for (int j = 0; j < 8; j++)
{
bool value = b & mask;
mask << 1;
}
}
Or using Java idioms
或使用 Java 习语
for (byte b : byteArray ) {
for ( int mask = 0x01; mask != 0x100; mask <<= 1 ) {
boolean value = ( b & mask ) != 0;
}
}
回答by akarnokd
I needed some bit streaming in my application. Hereyou can find my BitArray implementation. It is not a real iterator pattern but you can ask for 1-32 bits from the array in a streaming way. There is also an alternate implementation called BitReader later in the file.
我的应用程序中需要一些比特流。在这里你可以找到我的 BitArray 实现。它不是真正的迭代器模式,但您可以以流式方式从数组中请求 1-32 位。文件后面还有一个名为 BitReader 的替代实现。
回答by the.duckman
An alternative would be to use a BitInputStream like the one you can find hereand write code like this:
另一种方法是使用像您可以在此处找到的 BitInputStream并编写如下代码:
BitInputStream bin = new BitInputStream(new ByteArrayInputStream(bytes));
while(true){
int bit = bin.readBit();
// do something
}
bin.close();
(Note: Code doesn't contain EOFException or IOException handling for brevity.)
(注意:为简洁起见,代码不包含 EOFException 或 IOException 处理。)
But I'd go with Jon Skeets variant and do it on my own.
但我会选择 Jon Skeets 的变体并自己完成。
回答by amischiefr
I know, probably not the "coolest" way to do it, but you can extract each bit with the following code.
我知道,这可能不是“最酷”的方法,但您可以使用以下代码提取每一位。
int n = 156;
String bin = Integer.toBinaryString(n);
System.out.println(bin);
char arr[] = bin.toCharArray();
for(int i = 0; i < arr.length; ++i) {
System.out.println("Bit number " + (i + 1) + " = " + arr[i]);
}
10011100
10011100
Bit number 1 = 1
位号 1 = 1
Bit number 2 = 0
位号 2 = 0
Bit number 3 = 0
位号 3 = 0
Bit number 4 = 1
位号 4 = 1
Bit number 5 = 1
位号 5 = 1
Bit number 6 = 1
位号 6 = 1
Bit number 7 = 0
位号 7 = 0
Bit number 8 = 0
位号 8 = 0
回答by Matthew Flaschen
public class ByteArrayBitIterable implements Iterable<Boolean> {
private final byte[] array;
public ByteArrayBitIterable(byte[] array) {
this.array = array;
}
public Iterator<Boolean> iterator() {
return new Iterator<Boolean>() {
private int bitIndex = 0;
private int arrayIndex = 0;
public boolean hasNext() {
return (arrayIndex < array.length) && (bitIndex < 8);
}
public Boolean next() {
Boolean val = (array[arrayIndex] >> (7 - bitIndex) & 1) == 1;
bitIndex++;
if (bitIndex == 8) {
bitIndex = 0;
arrayIndex++;
}
return val;
}
public void remove() {
throw new UnsupportedOperationException();
}
};
}
public static void main(String[] a) {
ByteArrayBitIterable test = new ByteArrayBitIterable(
new byte[]{(byte)0xAA, (byte)0xAA});
for (boolean b : test)
System.out.println(b);
}
}

