Algorithm-BitVector

 view release on metacpan or  search on metacpan

Examples/BitVectorDemo.pl  view on Meta::CPAN

print "\nPrint out the first 64 bits read from the file:\n";
print "$bv1\n";               
          # 0100000100100000011010000111010101101110011001110111001001111001
print "\nRead the next 64 bits from the same file:\n";
$bv2 = $bv->read_bits_from_file(64);
print "$bv2\n";               
          # 0010000001100010011100100110111101110111011011100010000001100110
print "\nTake xor of the previous two bitvectors:\n";
$bv3 = $bv1 ^ $bv2;
print "$bv3\n";                
          # 0110000101000010000110100001101000011001000010010101001000011111

# Dividing an even-sized bitvector into two bitvectors:
print "\nExperiment with dividing an even-sized vector into two:\n";
($bv4,$bv5) = $bv3->divide_into_two();
print "$bv4\n";                           # 01100001010000100001101000011010
print "$bv5\n";                           # 00011001000010010101001000011111

# Permute a bitvector:
print "\nWe will use this bitvector for experiments with permute()\n";
$bv1 = Algorithm::BitVector->new( bitlist => [1, 0, 0, 1, 1, 0, 1] );
print "$bv1\n";                                        # 1001101
$bv2 = $bv1->permute( [6, 2, 0, 1] );
print "Permuted and contracted form of the previous bitvector:\n";
print "$bv2\n";                                        # 1010

# Write an internally generated bitvector to a disk file:
print "\nExperiment with writing an internally generated bitvector out to a disk file:\n";
$bv1 = Algorithm::BitVector->new( bitstring => '00001010' ); 
open my $FILEOUT, ">test.txt";
$bv1->write_to_file( $FILEOUT );
close $FILEOUT;
$bv2 = Algorithm::BitVector->new( filename => 'test.txt' );
$bv3 = $bv2->read_bits_from_file( 32 );
print "\nDisplay bitvectors written out to file and read back from the file and their respective lengths:\n";
print "$bv1      $bv3\n";                                      #   00001010      00001010
print length($bv1) . "             " . length($bv3) . "\n";    #   8             8

# Experiment with reading a file from beginning to end and constructing 64-bit bit
# vectors as you go along:
print "\nExperiments with reading a file from the beginning to end:\n";
$bv = Algorithm::BitVector->new( filename => 'testinput.txt' );
print "Here are all the bits read from the file:\n";
while ($bv->{more_to_read}) {
    my $bv_read = $bv->read_bits_from_file( 64 );
    print "$bv_read\n";
}
print "\n";
                  # 0100000100100000011010000111010101101110011001110111001001111001
                  # 0010000001100010011100100110111101110111011011100010000001100110
                  # 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:
print "\nExperiment in 64-bit permutation and unpermutation of the previous 64-bit bitvector:\n";
print "The permutation array was generated separately by the Fisher-Yates shuffle algorithm:\n";
$bv2 = $bv1->permute( [22, 47, 33, 36, 18, 6, 32, 29, 54, 62, 4,
                        9, 42, 39, 45, 59, 8, 50, 35, 20, 25, 49,
                       15, 61, 55, 60, 0, 14, 38, 40, 23, 17, 41,
                       10, 57, 12, 30, 3, 52, 11, 26, 43, 21, 13,
                       58, 37, 48, 28, 1, 63, 2, 31, 53, 56, 44, 24,
                       51, 19, 7, 5, 34, 27, 16, 46] );
print "Permuted bitvector:\n";
print "$bv2\n";      # 0111100110001011010111000100100111100000100011001101000010101101
$bv3 = $bv2->unpermute( [22, 47, 33, 36, 18, 6, 32, 29, 54, 62, 4,
                          9, 42, 39, 45, 59, 8, 50, 35, 20, 25, 49,
                         15, 61, 55, 60, 0, 14, 38, 40, 23, 17, 41,
                         10, 57, 12, 30, 3, 52, 11, 26, 43, 21, 13,
                         58, 37, 48, 28, 1, 63, 2, 31, 53, 56, 44, 24,
                         51, 19, 7, 5, 34, 27, 16, 46] );    
print "Result obtained by unpurmuting the permuted bitvector:\n";
print "$bv3\n";      # 0100000100100000011010000111010101101110011001110111001001111001

# Experiments with circular shifts to the left and to the right:
print "\nTry circular shifts to the left and to the right for the following bitvector:\n";
print "$bv3\n";      # 0100000100100000011010000111010101101110011001110111001001111001
print "Circular shift to the left by 7 positions:\n";
$bv3 = $bv3 << 7;
print "$bv3\n";      # 1001000000110100001110101011011100110011101110010011110010100000
print "\nCircular shift to the right by 7 positions:\n";
$bv3 = $bv3 >> 7;
print "$bv3\n";      # 0100000100100000011010000111010101101110011001110111001001111001
print "Test length on the above bitvector:   ";
print length($bv3) . "\n";                      # 64

print "\nExperiments with chained invocations of circular shifts:\n";
$bv = Algorithm::BitVector->new( bitlist => [1, 1, 1, 0, 0, 1] );
print "$bv\n";                                    # 111001
$bv = $bv >> 1;
print "$bv\n";                                    # 111100
$bv = $bv >> 1 >> 1;
print "$bv\n";                                    # 001111
$bv = Algorithm::BitVector->new( bitlist => [1, 1, 1, 0, 0, 1] );
print "$bv\n";                                    # 111001
$bv = $bv << 1;
print "$bv\n";                                    # 110011
$bv = $bv << 1 << 1;
print "$bv\n";                                    # 001111

# Experiment with extracting a slice (in the form of a list of bits) from a bitvector:
print "\nTest forming a [5..21] list-based slice of the above bitvector:\n";
# The following call will return bits indexed from 5 through 20 from the $bv3 bitvector:
my $arr = $bv3->get_bit( [5..21] );
print "@$arr\n";                         # 0 0 1 0 0 1 0 0 0 0 0 0 1 1 0 1



( run in 1.330 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )