I'm not sure I correctly interpreted your intended API on bit_set_seq(), and I know I didn't on bit_set_skip.

For bit_set_seq(), I interpreted the index to the array-ref as the bit number of the output, and the value at that index as the bit number for the input. If you intended the other way around, my tests below should work for exp0123 and exp3210, but won't be right for exp0312. But since my tests are completely failing, there's probably something I'm missing about the way you intended it to be used.

use Bit::Manip qw(:all); use Test::More; # out of order sequence my @exp0123 = (0..15); my @exp3210 = reverse(0..15); my @exp0312 = (0b0000,0b0001,0b0100,0b0101,0b1000,0b1001,0b1100,0b1101 +,0b0010,0b0011,0b0110,0b0111,0b1010,0b1011,0b1110,0b1111); for (0 .. 15) { is( bit_set(0x00, 2, 4, $_), $_ << 2, "nomal set, shifted up 2" ); is( bit_set_seq(0x00, [0,1,2,3], $_), $exp0123[$_], "[0,1,2,3]: I +expect bit 0 [LSB] of input to be transferred to bit 0 [LSB] of outpu +t -- ie, same order"); is( bit_set_seq(0x00, [3,2,1,0], $_), $exp3210[$_], "[3,2,1,0]: I +expect bit 3 [MSB] of input to be transferred to bit 0 [LSB] of outpu +t, in2 -> out1, in1 -> out2, in0 -> out3 -- ie, reverse the bits"); is( bit_set_seq(0x00, [0,3,1,2], $_), $exp0312[$_], "[0,3,1,2]: I +expect in0 -> out0, in3->out1, in1->out2, in2->out3"); } ... # your subs go below, not quoted here...

For bit_set_skip, I couldn't interpret it at all... What I would want would be a feature where I could put arbitrary gaps, for example, $out = bit_set_skip( $in, $seq, $val ). I would want to break $val into chunks based on seq, and put it into $in to create out. For this table: K means "keep the same bit value as in input", whereas a 1 or 0 means that I want that value there, regardless of what was originally in INPUT. I couldn't figure out the $seq necessary to do this, but below my table is the API I would have guessed to set this...

# INPUT VAL DESIRED OUTPUT # 0bKKKK_KKKK 0b0000_0000 0bKKK0_K000 # 0bKKKK_KKKK 0b0000_0001 0bKKK0_K001 # 0bKKKK_KKKK 0b0000_0010 0bKKK0_K010 # 0bKKKK_KKKK 0b0000_0011 0bKKK0_K011 # 0bKKKK_KKKK 0b0000_0100 0bKKK0_K100 # 0bKKKK_KKKK 0b0000_0101 0bKKK0_K101 # 0bKKKK_KKKK 0b0000_0110 0bKKK0_K110 # 0bKKKK_KKKK 0b0000_0111 0bKKK0_K111 # 0bKKKK_KKKK 0b0000_1000 0bKKK1_K000 # 0bKKKK_KKKK 0b0000_1001 0bKKK1_K001 # 0bKKKK_KKKK 0b0000_1010 0bKKK1_K010 # 0bKKKK_KKKK 0b0000_1011 0bKKK1_K011 # 0bKKKK_KKKK 0b0000_1100 0bKKK1_K100 # 0bKKKK_KKKK 0b0000_1101 0bKKK1_K101 # 0bKKKK_KKKK 0b0000_1110 0bKKK1_K110 # 0bKKKK_KKKK 0b0000_1111 0bKKK1_K111 for (0 .. 15) { is( bit_set_skip( 0x00, [0,1,2,4], $_), (($_ & 0x08)<<1) | (($_ & +0x07)<<0), "put a gap between the MSB and the lower three bits"); }

In reply to Re^3: Bit string manipulation made easy with Bit::Manip by pryrt
in thread Bit string manipulation made easy with Bit::Manip by stevieb

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.