How can i iterate bits in a byte array?
|
You'd have to write your own implementation of
(where It's not really hard - but a bit of a pain. Let me know if you'd like a sample implementation... |
|||
|
|
||||
|
Original:
Or using Java idioms
|
|||||||||||||
|
An alternative would be to use a BitInputStream like the one you can find here and write code like this:
(Note: Code doesn't contain EOFException or IOException handling for brevity.) But I'd go with Jon Skeets variant and do it on my own. |
|||
|
I know, probably not the "coolest" way to do it, but you can extract each bit with the following code.
10011100 Bit number 1 = 1 Bit number 2 = 0 Bit number 3 = 0 Bit number 4 = 1 Bit number 5 = 1 Bit number 6 = 1 Bit number 7 = 0 Bit number 8 = 0 |
|||||||||
|
I needed some bit streaming in my application. Here you 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. |
|||
|
You can iterate through the byte array, and for each byte use the bitwise operators to iterate though its bits. |
|||
|