I've done a mock up of what I think you're after. The function that handles the out of order sequence is bit_set_seq() below, and the one that'll handle missing bits (and leave them as is) is bit_set_skip(). Let me know if the output is what you expected. The implementation details aren't really relevant right now, as I just wanted to ensure the output was correct before I made it better.

use warnings; use strict; use feature 'say'; use Bit::Manip qw(:all); # out of order sequence for (0..11){ bit_set_seq(0x00, [3, 1, 2, 0], $_); } # skip over for (0..11){ my $b = $_; $b = bit_on($b, 1); $b = bit_on($b, 2); bit_set_skip($b, [0, 3], $_); } sub bit_set_seq { my ($b, $seq, $val) = @_; my $lsb = (sort @$seq)[0]; my $msb = (sort @$seq)[-1]; my $nbits = ($msb - $lsb); $b = bit_set($b, $lsb, $nbits, $val); say "** $val"; printf("seq before: %b\n", $b); my $x = $b; for (0..$nbits){ my $o = bit_get($b, $_, $_); my $s = bit_get($b, $seq->[$_], $seq->[$_]); if ($o != $s){ $x = bit_set($x, $_, 1, $s); } } printf("seq after %b\n\n", $x); } sub bit_set_skip { my ($b, $seq, $val) = @_; my $lsb = (sort @$seq)[0]; my $msb = (sort @$seq)[-1]; my @leave_bits = map $seq->[$_-1]+1..$seq->[$_]-1, 1..@$seq-1; my %leave_vals = map {$_ => bit_get($b, $_, $_)} @leave_bits; my $nbits = ($msb - $lsb) + 1; $b = bit_set($b, $lsb, $nbits, $val); say "** $val"; printf("skip before: %b\n", $b); for (keys %leave_vals){ $b = bit_set($b, $_, 1, $leave_vals{$_}); } printf("skip after: %b\n\n", $b); }

output:

** 0 seq before: 0 seq after 0 ** 1 seq before: 1 seq after 1000 ** 2 seq before: 10 seq after 10 ** 3 seq before: 11 seq after 1010 ** 4 seq before: 100 seq after 100 ** 5 seq before: 101 seq after 1100 ** 6 seq before: 110 seq after 110 ** 7 seq before: 111 seq after 1110 ** 8 seq before: 1000 seq after 1 ** 9 seq before: 1001 seq after 1001 ** 10 seq before: 1010 seq after 11 ** 11 seq before: 1011 seq after 1011 ** 0 skip before: 0 skip after: 110 ** 1 skip before: 1 skip after: 111 ** 2 skip before: 10 skip after: 110 ** 3 skip before: 11 skip after: 111 ** 4 skip before: 100 skip after: 110 ** 5 skip before: 101 skip after: 111 ** 6 skip before: 110 skip after: 110 ** 7 skip before: 111 skip after: 111 ** 8 skip before: 1000 skip after: 1110 ** 9 skip before: 1001 skip after: 1111 ** 10 skip before: 1010 skip after: 1110 ** 11 skip before: 1011 skip after: 1111

In reply to Re^2: Bit string manipulation made easy with Bit::Manip by stevieb
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.