[ACCEPTED]-Java Iterate Bits in Byte Array-loops

Accepted answer
Score: 43

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

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

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

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

Score: 18
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);
    }
}

0

Score: 10

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

for (byte b : byteArray ) {
  for ( int mask = 0x01; mask != 0x100; mask <<= 1 ) {
      boolean value = ( b & mask ) != 0;
  }
}

0

Score: 2

An alternative would be to use a BitInputStream 5 like the one you can find here and write code 4 like this:

BitInputStream bin = new BitInputStream(new ByteArrayInputStream(bytes));
    while(true){
        int bit = bin.readBit();
        // do something
    }
bin.close();

(Note: Code doesn't contain EOFException 3 or IOException handling for brevity.)

But 2 I'd go with Jon Skeets variant and do it 1 on my own.

Score: 1

I needed some bit streaming in my application. Here you 5 can find my BitArray implementation. It 4 is not a real iterator pattern but you can 3 ask for 1-32 bits from the array in a streaming 2 way. There is also an alternate implementation 1 called BitReader later in the file.

Score: 1

I know, probably not the "coolest" way to 5 do it, but you can extract each bit with 4 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

Bit number 1 3 = 1

Bit number 2 = 0

Bit number 3 = 0

Bit number 2 4 = 1

Bit number 5 = 1

Bit number 6 = 1

Bit 1 number 7 = 0

Bit number 8 = 0

Score: 0

You can iterate through the byte array, and 2 for each byte use the bitwise operators 1 to iterate though its bits.

Score: 0

Alternatively, you can use BitSet for this:

byte[] bytes=...;
BitSet bitSet=BitSet.valueOf(bytes);
for(int i=0;i<bitSet.length();i++){
    boolean bit=bitSet.get(i);
    //use your bit
} 

0

More Related questions