FAQ 4.53 How do I manipulate arrays of bits?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

This message is one of several periodic postings to comp.lang.perl.misc
intended to make it easier for perl programmers to find answers to
common questions. The core of this message represents an excerpt
from the documentation provided with Perl.


4.53: How do I manipulate arrays of bits?

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

    For example, this sets $vec to have bit N set if $ints[N] was set:

        $vec = '';
        foreach(@ints) { vec($vec,$_,1) = 1 }

    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/// / length $vec > 0.95) {
                use integer;
                my $i;
                # This method is faster with mostly null-bytes
                while($vec =~ /[^]/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 =~ /[^]+/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);
        @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";




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

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


Documents such as this have been called "Answers to Frequently
Asked Questions" or FAQ for short.  They represent an important
part of the Usenet tradition.  They serve to reduce the volume of
redundant traffic on a news group by providing quality answers to
questions that keep coming up.

If you are some how irritated by seeing these postings you are free
to ignore them or add the sender to your killfile.  If you find
errors or other problems with these postings please send corrections
or comments to the posting email address or to the maintainers as
directed in the perlfaq manual page.

Note that the FAQ text posted by this server may have been modified
from that distributed in the stable Perl release.  It may have been
edited to reflect the additions, changes and corrections provided
by respondents, reviewers, and critics to previous postings of
these FAQ. Complete text of these FAQ are available on request.

The perlfaq manual page contains the following copyright notice.


    Copyright (c) 1997-2002 Tom Christiansen and Nathan
    Torkington, and other contributors as noted. All rights

This posting is provided in the hope that it will be useful but
does not represent a commitment or contract of any kind on the part
of the contributers, authors or their agents.

Site Timeline