|There's more than one way to do things|
I've muddled my bit and byte formats.by murrayn (Sexton)
|on Oct 09, 2018 at 02:10 UTC||Need Help??|
murrayn has asked for the
wisdom of the Perl Monks concerning the following question:
I have a need to read a two dimensional matrix of BITS one column at a time. (For the curious, it's a TIFF file but that's not important.)
The data is stored in a binary "chunk" with parameters telling me the physical storage represents Y rows of X BYTES each. I read the chunk into a Perl array with each element containing one row of the file. As a result, "$binimage[$row]" contains a string of bits as wide as my image - one per pixel.
To transform this data into a required format I actually need to read columns of BITS in reverse order - I want row 5/bit 0, row 4/bit 0, … row 0/bit 0 … row 5/bit 1 … row 0/bit 1 … row 0/bit 7 etc., until the end of the file. (Let's not go into why!)
Snippets of code and results...
Generates this output
I know from my hex editor that the first BYTE of rows 5..1 contain 0b00000000 and the first BYTE of row 0 contains 0b10010000 which matches unpack('B8', …).
I expect that my bitwise & of 0b10010000 with 0x80 should produce 0x80 which will be "TRUE" which should tell me that the first bit of the byte is set.
I expect that, similarly, bitwise & of 0b00000000 with 0x80 will be 0 indicating that the first bit of the byte is not set.
Why is this demonstrably not so?
Why does my unpack of 0x80 generate the pattern "00110001" when unpack of 0x00 from the file generates "00000000"?