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...
(some variables, $nextrow, $col, $subcol, are set in outer loops not shown)

for my $index (0..5) { my $thisrow = $nextrow - $index; print "processing row/column/bit $thisrow/$col/$subcol: " . substr +($binimage[$thisrow], $col, 1) . "\n"; printf ("Incoming byte is %d/%x/%8b/%s. Test bitmask is %d/%x/%8b +/%s.\n", substr($binimage[$thisrow], $col, 1),substr($binimage[$thisrow], $ +col, 1),substr($binimage[$thisrow], $col, 1), unpack('B8',substr($binimage[$thisrow], $col, 1)), 2**(7-$subcol), 2**(7-$subcol), 2**(7-$subcol), unpack('B8', 2**(7-$subcol))); substr ($char, $index + 2, 1) = ((substr($binimage[$thisrow], $col +, 1)) & 2**(7-$subcol))? "1" : "0"; # Update tracker where a "1" is +found! }
Generates this output
processing row/column/bit 5/0/0: (unprintable square box symbol) Incoming byte is 0/0/ 0/00000000. Test bitmask is 128/80/100000 +00/00110001. processing row/column/bit 4/0/0: (unprintable square box symbol) Incoming byte is 0/0/ 0/00000000. Test bitmask is 128/80/100000 +00/00110001. processing row/column/bit 3/0/0: (unprintable square box symbol) Incoming byte is 0/0/ 0/00000000. Test bitmask is 128/80/100000 +00/00110001. processing row/column/bit 2/0/0: (unprintable square box symbol) Incoming byte is 0/0/ 0/00000000. Test bitmask is 128/80/100000 +00/00110001. processing row/column/bit 1/0/0: (unprintable square box symbol) Incoming byte is 0/0/ 0/00000000. Test bitmask is 128/80/100000 +00/00110001. processing row/column/bit 0/0/0: ? Incoming byte is 0/0/ 0/10010000. Test bitmask is 128/80/100000 +00/00110001. ...

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"?


In reply to I've muddled my bit and byte formats. by murrayn

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.