FAQ 4.53 How do I manipulate arrays of bits?

Discussion in 'Perl Misc' started by PerlFAQ Server, Feb 10, 2011.

  1. This is an excerpt from the latest version perlfaq4.pod, which
    comes with the standard Perl distribution. These postings aim to
    reduce the number of repeated questions as well as allow the community
    to review and update the answers. The latest version of the complete
    perlfaq is at http://faq.perl.org .

    --------------------------------------------------------------------

    4.53: How do I manipulate arrays of bits?

    Use "pack()" and "unpack()", or else "vec()" and the bitwise operations.

    For example, you don't have to store individual bits in an array (which
    would mean that you're wasting a lot of space). To convert an array of
    bits to a string, use "vec()" to set the right bits. This sets $vec to
    have bit N set only if $ints[N] was set:

    @ints = (...); # array of bits, e.g. ( 1, 0, 0, 1, 1, 0 ... )
    $vec = '';
    foreach( 0 .. $#ints ) {
    vec($vec,$_,1) = 1 if $ints[$_];
    }

    The string $vec only takes up as many bits as it needs. For instance, if
    you had 16 entries in @ints, $vec only needs two bytes to store them
    (not counting the scalar variable overhead).

    Here's how, given a vector in $vec, you can get those bits into your
    @ints array:

    sub bitvec_to_list {
    my $vec = shift;
    my @ints;
    # Find null-byte density then select best algorithm
    if ($vec =~ tr/\0// / length $vec > 0.95) {
    use integer;
    my $i;

    # This method is faster with mostly null-bytes
    while($vec =~ /[^\0]/g ) {
    $i = -9 + 8 * pos $vec;
    push @ints, $i if vec($vec, ++$i, 1);
    push @ints, $i if vec($vec, ++$i, 1);
    push @ints, $i if vec($vec, ++$i, 1);
    push @ints, $i if vec($vec, ++$i, 1);
    push @ints, $i if vec($vec, ++$i, 1);
    push @ints, $i if vec($vec, ++$i, 1);
    push @ints, $i if vec($vec, ++$i, 1);
    push @ints, $i if vec($vec, ++$i, 1);
    }
    }
    else {
    # This method is a fast general algorithm
    use integer;
    my $bits = unpack "b*", $vec;
    push @ints, 0 if $bits =~ s/^(\d)// && $1;
    push @ints, pos $bits while($bits =~ /1/g);
    }

    return \@ints;
    }

    This method gets faster the more sparse the bit vector is. (Courtesy of
    Tim Bunce and Winfried Koenig.)

    You can make the while loop a lot shorter with this suggestion from
    Benjamin Goldberg:

    while($vec =~ /[^\0]+/g ) {
    push @ints, grep vec($vec, $_, 1), $-[0] * 8 .. $+[0] * 8;
    }

    Or use the CPAN module "Bit::Vector":

    $vector = Bit::Vector->new($num_of_bits);
    $vector->Index_List_Store(@ints);
    @ints = $vector->Index_List_Read();

    "Bit::Vector" provides efficient methods for bit vector, sets of small
    integers and "big int" math.

    Here's a more extensive illustration using vec():

    # vec demo
    $vector = "\xff\x0f\xef\xfe";
    print "Ilya's string \\xff\\x0f\\xef\\xfe represents the number ",
    unpack("N", $vector), "\n";
    $is_set = vec($vector, 23, 1);
    print "Its 23rd bit is ", $is_set ? "set" : "clear", ".\n";
    pvec($vector);

    set_vec(1,1,1);
    set_vec(3,1,1);
    set_vec(23,1,1);

    set_vec(3,1,3);
    set_vec(3,2,3);
    set_vec(3,4,3);
    set_vec(3,4,7);
    set_vec(3,8,3);
    set_vec(3,8,7);

    set_vec(0,32,17);
    set_vec(1,32,17);

    sub set_vec {
    my ($offset, $width, $value) = @_;
    my $vector = '';
    vec($vector, $offset, $width) = $value;
    print "offset=$offset width=$width value=$value\n";
    pvec($vector);
    }

    sub pvec {
    my $vector = shift;
    my $bits = unpack("b*", $vector);
    my $i = 0;
    my $BASE = 8;

    print "vector length in bytes: ", length($vector), "\n";
    @bytes = unpack("A8" x length($vector), $bits);
    print "bits are: @bytes\n\n";
    }



    --------------------------------------------------------------------

    The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
    are not necessarily experts in every domain where Perl might show up,
    so please include as much information as possible and relevant in any
    corrections. The perlfaq-workers also don't have access to every
    operating system or platform, so please include relevant details for
    corrections to examples that do not work on particular platforms.
    Working code is greatly appreciated.

    If you'd like to help maintain the perlfaq, see the details in
    perlfaq.pod.
     
    PerlFAQ Server, Feb 10, 2011
    #1
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. GGG
    Replies:
    10
    Views:
    13,402
    Donar
    Jul 6, 2006
  2. Dave
    Replies:
    1
    Views:
    481
    Mike Wahler
    Jan 22, 2005
  3. sarmin kho
    Replies:
    2
    Views:
    1,061
    A. Lloyd Flanagan
    Jun 15, 2004
  4. Miki Tebeka
    Replies:
    1
    Views:
    616
    Marcin 'Qrczak' Kowalczyk
    Jun 14, 2004
  5. sergey

    "casting" bits to bits?

    sergey, Nov 8, 2006, in forum: VHDL
    Replies:
    1
    Views:
    954
    sergey
    Nov 8, 2006
  6. Bryan Parkoff

    Manipulate two 8-Bits using Carry?

    Bryan Parkoff, Sep 6, 2005, in forum: C++
    Replies:
    0
    Views:
    339
    Bryan Parkoff
    Sep 6, 2005
  7. Philipp
    Replies:
    21
    Views:
    1,560
    Philipp
    Jan 20, 2009
  8. PerlFAQ Server

    FAQ 4.53 How do I manipulate arrays of bits?

    PerlFAQ Server, Mar 24, 2011, in forum: Perl Misc
    Replies:
    0
    Views:
    199
    PerlFAQ Server
    Mar 24, 2011
Loading...