Algorithm-BitVector
view release on metacpan or search on metacpan
Examples/BitVectorDemo.pl view on Meta::CPAN
print $bv->count_bits() . "\n"; # 4
$bv = Algorithm::BitVector->new( bitstring => '00111000' );
print $bv->count_bits() . "\n"; # 3
$bv = Algorithm::BitVector->new( bitstring => '001' );
print $bv->count_bits() . "\n"; # 1
$bv = Algorithm::BitVector->new( bitstring => '00000000000000' );
print $bv->count_bits() . "\n"; # 0
# Experiment with the fast algorithm for counts the set bits in large but sparse bitvectors:
print "\nTesting count_bits_sparse() on a vector of TWO MILLION bits (be patient):\n";
$bv = Algorithm::BitVector->new( size => 2000000 );
$bv->set_bit(345234, 1);
$bv->set_bit(233, 1);
$bv->set_bit(243, 1);
$bv->set_bit(18, 1);
$bv->set_bit(785, 1);
print "The number of bits set: " . $bv->count_bits_sparse() . "\n"; # 5
# Experiments with resetting the internals of a bitvector:
print "\nTest set_value() method:\n";
$bv = Algorithm::BitVector->new( intVal => 7, size => 16 );
print "$bv\n"; # 0000000000000111
$bv->set_value( intVal => 45 );
print "$bv\n"; # 101101
# Experiment with Jaccard similarity and distance and with Hamming distance:
print "\nTesting Jaccard similarity and distance and Hamming distance:\n";
$bv1 = Algorithm::BitVector->new( bitstring => '11111111' );
$bv2 = Algorithm::BitVector->new( bitstring => '00101011' );
print "Jaccard similarity: " . $bv1->jaccard_similarity( $bv2 ) . "\n"; # 0.5
print "Jaccard distance: " . $bv1->jaccard_distance( $bv2 ) . "\n"; # 0.5
print "Hamming distance: " . $bv1->hamming_distance( $bv2 ) . "\n"; # 4
# Experiments in finding the position of the next set bit after a given position:
print "\nTesting next_set_bit():\n";
$bv = Algorithm::BitVector->new( bitstring => '00000000000001' );
print $bv->next_set_bit(5) . "\n"; # 13
$bv = Algorithm::BitVector->new( bitstring => '000000000000001' );
print $bv->next_set_bit(5) . "\n"; # 14
$bv = Algorithm::BitVector->new( bitstring => '0000000000000001' );
print $bv->next_set_bit(5) . "\n"; # 15
$bv = Algorithm::BitVector->new( bitstring => '00000000000000001' );
print $bv->next_set_bit(5) . "\n"; # 16
# The rank of a bit is the number of bits set prior to that bit:
print "\nTesting rank_of_bit_set_at_index():\n";
$bv = Algorithm::BitVector->new( bitstring => '01010101011100' );
print $bv->rank_of_bit_set_at_index( 10 ) . "\n"; # 6
# Experiment with determining whether the int value of a bitvector is a power 2:
print "\nTesting is_power_of_2():\n";
$bv = Algorithm::BitVector->new( bitstring => '10000000001110' );
print "int value: " . int($bv) . "\n"; # 826
print $bv->is_power_of_2() . "\n"; # 0
print "\nTesting is_power_of_2_sparse():\n";
print $bv->is_power_of_2_sparse() . "\n"; # 0
# Experiment with reversing a bitvector:
print "\nTesting reverse():\n";
$bv = Algorithm::BitVector->new( bitstring => '0001100000000000001' );
print "original bv: $bv\n"; # 0001100000000000001
print "reversed bv: " . $bv->reverse() . "\n"; # 1000000000000011000
# Calculate the GCD of two bitvectors using Euclid's algorithm on the integers values:
print "\nTesting Greatest Common Divisor (gcd):\n";
$bv1 = Algorithm::BitVector->new( bitstring => '01100110' );
print "first arg bv: $bv1 of int value: " . int($bv1) . "\n"; #102
$bv2 = Algorithm::BitVector->new( bitstring => '011010' );
print "second arg bv: $bv2 of int value: " . int($bv2) . "\n"; # 26
$bv = $bv1->gcd( $bv2 );
print "gcd bitvec is: $bv of int value: " . int($bv) . "\n"; # 2
# Calculate the multiplicative inverse of a bitvector with respect to a modulus vector:
print "\nTesting multiplicative_inverse:\n";
my $bv_modulus = Algorithm::BitVector->new( intVal => 32 );
print "modulus is bitvec: $bv_modulus of int value: " . int($bv_modulus) . "\n";
$bv = Algorithm::BitVector->new( intVal => 17 );
print "bv: $bv of int value: " . int($bv) . "\n";
$result = $bv->multiplicative_inverse( $bv_modulus );
if ($result) {
print "MI bitvec is: $result of int value: " . int($result) . "\n"; # 17
} else {
print "No multiplicative inverse in this case\n";
}
# Experiments with non-circular shifts to the left and to the right:
print "\nExperiments with regular and chained invocations of NON-circular shifts:\n";
$bv = Algorithm::BitVector->new( bitlist => [1,1, 1, 0, 0, 1] );
print "$bv\n"; # 111001
$bv->shift_right(1);
print "$bv\n"; # 011100
$bv->shift_right(1);
print "$bv\n"; # 001110
$bv->shift_right(1)->shift_right(1);
print "$bv\n"; # 000011
$bv->shift_right(1);
print "$bv\n"; # 000001
$bv = Algorithm::BitVector->new( bitlist => [1,1, 1, 0, 0, 1] );
print "$bv\n"; # 111001
$bv->shift_left(1);
print "$bv\n"; # 110010
$bv->shift_left(1);
print "$bv\n"; # 100100
$bv->shift_left(1)->shift_left(1);
print "$bv\n"; # 010000
$bv->shift_left(1);
print "$bv\n"; # 100000
# Experiment with the calculation of multiplication in a Galois Field GF(2);
print "\nTest multiplication in GF(2):\n";
$a = Algorithm::BitVector->new( bitstring => '0110001' );
$b = Algorithm::BitVector->new( bitstring => '0110' );
my $c = $a->gf_multiply($b);
print "Product of a = $a with b = $b is $c\n";
# Product of a = 0110001 with b = 0110 is 00010100110
# Experiment with dividing one vector by another in GF(2^n):
print "\nTest division in GF(2^n):\n";
my $mod = Algorithm::BitVector->new( bitstring => '100011011' ); # AES modulus
my $n = 8;
$a = Algorithm::BitVector->new( bitstring => '11100010110001' );
( run in 1.601 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )