Algorithm-BitVector
view release on metacpan or search on metacpan
lib/Algorithm/BitVector.pm view on Meta::CPAN
=item B<Constructing a bitvector by specifying both the size and the integer values:>
As mentioned, with the C<intVal> option, you get the smallest possible bitvector
that can be generated from that integer. If you want a I<larger> bitvector, you can
also supply the C<size> option as shown below:
$bv = Algorithm::BitVector->new( intVal => 5006, size => 16 ); # 0001001110001110
If the value supplied for the C<size> option in such a call is not larger than the
smallest bit array that represents the C<intVal> value, the constructor will throw an
exception.
=item B<Constructing a bitvector from a very large integer:>
use Math::BigInt;
$x = Math::BigInt->new('12345678901234567890123456789012345678901234567890');
$bv = Algorithm::BitVector->new( intVal => $x );
#1000011100100111111101100011011010011010101011111000001111001010000\
#10101000000100110011101000111101011111000110001111111000110010110110\
#01110001111110000101011010010
lib/Algorithm/BitVector.pm view on Meta::CPAN
=head3 get_bitvector_in_ascii()
=over 4
$bv = Algorithm::BitVector->new( textstring => "hello" );
print "$bv\n"; # 0110100001100101011011000110110001101111
print $bv->get_bitvector_in_ascrii(); # hello
The method returns a string of ASCII characters by converting successive 8-bit slices
of the bitvector into an ASCII character. It throws an exception if the size of the
bit pattern is not a multiple of 8. Calling this method to create a text-based print
representation of a bit vector makes sense only if you don't expect to see any
unprintable characters in the successive 8-bit slices of the bitvector. Let's say
you have created a bitvector from a text string using the appropriate constructor
call. Subsequently, you encrypted this text string. Next, you or someone else
decrypts the encrypted bit stream. Since what comes out at the decryption end must
be the original text string, it would make sense to invoke this method to recover the
original text.
=back
lib/Algorithm/BitVector.pm view on Meta::CPAN
=over 4
Assuming that length of your bitvector is a multiple of 4, this methods returns a
hex representation of the bit pattern:
$bv = Algorithm::BitVector->new(bitstring => "0110100001100101011011000110110001101111");
print $bv->get_bitvector_in_hex(); # 68656c6c6f
The hex representation is returned in the form if a string of hex characters. This
method throws an exception if the size of the bitvector is not a multiple of 4.
=back
=head3 get_slice()
=over 4
You can use this method to get a slice of a BitVector that is within a specified range.
You can specify the index range with the usual range operator in Perl. If the index
range is, say, '5..11', the method will return all bits at index values 5 through 10.
lib/Algorithm/BitVector.pm view on Meta::CPAN
=over 4
Hamming distance is commonly used to measure dissimilarity between two bitvectors of
the same size.
$bv1 = Algorithm::BitVector->new( bitstring => '11111111' );
$bv2 = Algorithm::BitVector->new( bitstring => '00101011' );
print $bv1->hamming_distance( $bv2 ); # 4
This distance returns the number of bit positions in which two the bit patterns
differ. The method throws an exception if the two bitvectors are not of the same
length. The value returned is an integer.
=back
=head3 int_value()
=over 4
You can find the integer value of a bitvector by
lib/Algorithm/BitVector.pm view on Meta::CPAN
=over 4
You can measure the "rank" of a bit that is set at a given index. Rank is the number
of bits that are set up to the argument position, as in
$bv = Algorithm::BitVector->new( bitstring => '01010101011100' );
print $bv->rank_of_bit_set_at_index( 10 ); # 6
The value 6 returned by this call to C<rank_of_bit_set_at_index()> is the number of
bits set up to the position indexed 10 (including that position). The method throws
an exception if there is no bit set at the argument position.
=back
=head3 read_bits_from_file()
=over 4
Constructing bitvectors from the contents of a disk file takes two steps: First you
must make the call shown in the first statement below. The purpose of this call is to
lib/Algorithm/BitVector.pm view on Meta::CPAN
as shown below:
$bv1 = Algorithm::BitVector->new( intVal => 203, size => 8 );
print $bv1; # 11001011
$bv2 = $bv1->permute( [3, 2, 1, 0, 7, 6, 5, 4] );
print $bv2; # 00111101
$bv3 = $bv2->unpermute( [3, 2, 1, 0, 7, 6, 5, 4] );
print $bv3; # 11001011
The method returns a new bitvector with unpermuted bits. Also note that the method
throws an exception if the permutation list is not as long as the size of the
bitvector on which the method is invoked.
=back
=head3 write_to_file()
=over 4
This method writes the bitvectors in your program to a disk file:
$bv1 = Algorithm::BitVector->new( bitstring => '00001010' );
open my $FILEOUT, ">test.txt";
$bv1->write_to_file( $FILEOUT );
close $FILEOUT;
The size of a bitvector must be a multiple of 8 for this write method to work. If
this condition is not met, the method will throw an exception.
B<Important for Windows Users:> When writing an internally generated bitvector out
to a disk file, it may be important to open the file in the binary mode, since
otherwise the bit pattern `00001010' ('\n') in your bitstring will be written out as
0000110100001010 ('\r\n') which is the line break on Windows machines.
=back
=head1 REQUIRED
( run in 0.300 second using v1.01-cache-2.11-cpan-496ff517765 )