The comment in the  extract() function below says it works for a binary mask in the range 0 .. 255 (i.e., an 8-bit mask, and it does), but I think it will work with up to a 31-bit mask (but this is untested). The test suite is not complete; add your own examples.

use warnings; use strict; use Test::More 'no_plan' ; use Test::NoWarnings; VECTOR: for my $ar_vector ( # list of # string mask extracted chars "normal cases", [ 'ABCD', 0, ], [ 'ABCD', 1, qw( D) ], [ 'ABCD', 2, qw( C ) ], [ 'ABCD', 3, qw( C D) ], [ 'ABCD', 4, qw( B ) ], [ 'WXYZABCD', 8, qw( A ) ], [ 'WXYZABCD', 9, qw( A D) ], [ 'WXYZABCD', 15, qw( A B C D) ], [ 'WXYZABCD', 16, qw( Z ) ], [ 'WXYZABCD', 17, qw( Z D) ], [ 'WXYZABCD', 0b00011111, qw( Z A B C D) ], [ 'WXYZABCD', 0b00100000, qw( Y ) ], [ 'WXYZABCD', 0b00100001, qw( Y D) ], [ 'WXYZABCD', 254, qw(W X Y Z A B C ) ], [ 'WXYZABCD', 255, qw(W X Y Z A B C D) ], [ 'WXYZABCD', 256, qw( ) ], # one beyond "limited numification of decimal strings works", [ 'WXYZABCD', '0', ], [ 'WXYZABCD', '255', qw(W X Y Z A B C D) ], [ 'WXYZABCD', '256', qw( ) ], # one beyond "some degenerate cases", [ '', 0, qw() ], [ '', 1, qw() ], # one beyond [ '', 2, qw() ], # two beyond [ 'A', 0, qw( ) ], [ 'A', 1, qw(A) ], [ 'A', 2, qw( ) ], # one beyond [ 'A', 3, qw(A) ], # one beyond ) { if (ref $ar_vector ne 'ARRAY') { # note/comment if not vector note $ar_vector; next VECTOR; } my ($string, $mask, @expected) = @$ar_vector; is_deeply [ extract($string, $mask) ], \@expected; # my @got = extract($string, $mask); # my $cmt = # "'$string' mask $mask got(@got) expect(@expected)"; # is_deeply \@got, \@expected, $cmt; } # subroutines ###################################################### sub extract { my ($string, # string to extract chars from, given binary mask $mask, # integer (0 .. 255) representing binary mask ) = @_; my @characters = split //, $string; $mask = 0+$mask; # numify mask (redundant?) my $bit = 1 << @characters; # moving single-bit sub-mask return map { $characters[$_] } # 3. return the char. grep { $mask & ($bit >>= 1) } # 2. if its bit not 0... 0 .. $#characters # 1. each char in string... ; }

In reply to Re: Reading Binary by AnomalousMonk
in thread Reading Binary by BoulderBuff64

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.