Algorithm-BitVector
view release on metacpan or search on metacpan
Examples/BitVectorDemo.pl view on Meta::CPAN
#!/usr/bin/perl -w
#use lib '../blib/lib', '../blib/arch';
## BitVectorDemo.pl
use strict;
use Algorithm::BitVector 1.26;
# Construct an EMPTY bitvector (a bitvector of size 0):
print "\nConstructing an EMPTY bitvector (a bitvector of size 0):\n";
my $bv1 = Algorithm::BitVector->new( size => 0 );
print "$bv1\n"; # no output
# Construct a bitvector of size 2:
print "\nConstructing a bitvector of size 2:\n";
my $bv2 = Algorithm::BitVector->new( size => 2 );
print "$bv2\n"; # 00
# Joining two bitvectors:
print "\nConcatenating two previously constructed bitvectors:\n";
my $result = $bv1 + $bv2;
print "$result\n"; # 00
# The following works because Perl implicitly first stringyfies each
# argument before invoking the `.' operator
$result = $bv1 . $bv2;
print "$result\n"; # 00
# Construct a bitvector with a list of bits:
print "\nConstructing a bitvector from a list of bits:\n";
my $bv = Algorithm::BitVector->new( bitlist => [1, 1, 0, 1] );
print "$bv\n"; # 1101
# Construct a bitvector from an integer
$bv = Algorithm::BitVector->new( intVal => 5678 );
print "\nbitvector constructed from integer 5678:\n";
print "$bv\n"; # 1011000101110
print "\nbitvector constructed from integer 0:\n";
$bv = Algorithm::BitVector->new( intVal => 0 );
print "$bv\n"; # 0
print "\nbitvector constructed from integer 2:\n";
$bv = Algorithm::BitVector->new( intVal => 2 );
print "$bv\n"; # 10
print "\nbitvector constructed from integer 3:\n";
$bv = Algorithm::BitVector->new( intVal => 3 );
print "$bv\n"; # 11
print "\nbitvector constructed from integer 123456:\n";
$bv = Algorithm::BitVector->new( intVal => 123456 );
print "$bv\n"; # 11110001001000000
print "\nInt value of the previous bitvector as computed by int_value():\n";
print int($bv) . "\n"; # 123456
# Construct a bitvector from a very large integer:
use Math::BigInt;
my $x = Math::BigInt->new('12345678901234567890123456789012345678901234567890');
$bv = Algorithm::BitVector->new( intVal => $x );
print "\nHere is a bitvector constructed from a very large integer:\n";
print "$bv\n";
printf "The integer value of the above bitvector shown as a string is: %s\n", $bv->int_value();
print "Size of the bitvector: " . $bv->length() . "\n";
# Construct a bitvector of a specified length from a large integer:
( run in 2.349 seconds using v1.01-cache-2.11-cpan-cdf2f3d4e48 )