Algorithm-BitVector

 view release on metacpan or  search on metacpan

Examples/BitVectorDemo.pl  view on Meta::CPAN

                  # 0110111101111000001000000110101001110101011011010111000001100101
                  # 0110010000100000011011110111011001100101011100100010000001100001
                  # 0010000001101100011000010111101001111001001000000111011101101001
                  # 0110110001100100001000000110010001101111011001110010111000001010
                  # 0000101001100001011000010110000101100001011000010110000101100001
                  # 0110000101100001011000010110000101100001000010100000101001101010
                  # 0110101001101010011010100110101001101010011010100110101001101010
                  # 0110101001101010011010100110101001101010011010100110101001101010
                  # 0000101000001010

print "\nExperiment with closing a file object and start extracting bitvectors from the file from the beginning again:\n";
$bv->close_file_handle();
$bv = Algorithm::BitVector->new( filename => 'testinput.txt' );
$bv1 = $bv->read_bits_from_file(64);
print "Here are all the first 64 bits read from the file again after the file object was closed and opened again:\n";
print "$bv1\n";      # 0100000100100000011010000111010101101110011001110111001001111001
open $FILEOUT, ">testinput5.txt";
$bv1->write_to_file( $FILEOUT );
close $FILEOUT; 

# Experiments in 64-bit permutation and unpermutation:

lib/Algorithm/BitVector.pm  view on Meta::CPAN

    my $shift = $posn & 0xF;
    my $cv = $self->{_vector}->[$block_index];
    if ( ( ( $cv >> $shift ) & 1 ) != $val) {
        $self->{_vector}->[$block_index] = $cv ^ (1 << $shift);
    }
}

##  Get the bit from the designated position. This method can also return a slice of a
##  bitvector.  HOWEVER, NOTE THAT THE SLICE IS RETURNED AS A LIST OF THE BITS IN THE
##  INDEX RANGE YOU SPECIFIED. You can either easily convert the list of bits returned
##  into a bitvector in your own code or, starting with Version 1.26, you can call the
##  get_slice() method.
sub get_bit {
    my $self = shift;
    my $pos = shift;
    unless (ref($pos) eq "ARRAY") {
        die "index range error" if  ($pos >= $self->{size}) or ($pos < - $self->{size});
        $pos = $self->{size} + $pos if $pos < 0;
        return ( $self->{_vector}->[int($pos/16)] >> ($pos&15) ) & 1;
    } 
#    my @slice = map $self->get_bit($_), (@$pos)[0..@$pos-1];



( run in 0.280 second using v1.01-cache-2.11-cpan-0d8aa00de5b )