This is an example of a bit vector (a vector of "0"s or "1"s)

## Define a bit vector with positions 4 to 7 set to 1 my $bv1=''; vec($bv1,$_,1)=1 foreach (3..6); dumpbv($bv1); sub dumpbv { my $bitv = shift @_; my $pos=0; foreach (split "",unpack("b*",$bitv)){ print; $pos++; } }

With bit vectors you can do bit operations. for example, to find the overlapping ("OR") of 2 bit vectors:

sub sumbv { return $_[0] | $_[1]; }

... and the AND operation:

sub cmpbv { return $_[0] & $_[1]; }

A full working dummy example with these functions would be:

use strict; use warnings; ## Define a bit vector with positions 4 to 7 set to 1 my $bv1=''; vec($bv1,$_,1)=1 foreach (3..6); dumpbv ($bv1); print " bv1\n"; # Define a bit vector with positions 3 to 5 set to 1 my $bv2=''; vec($bv2,$_,1)=1 foreach (2..4); dumpbv ($bv2); print " bv2\n"; print "--------\n"; my $bv_sum = sumbv($bv1,$bv2); dumpbv ($bv_sum);print " OR\n"; my $bv_cmp = cmpbv($bv1,$bv2); dumpbv ($bv_cmp);print " AND\n"; sub sumbv { return $_[0] | $_[1]; } sub cmpbv { return $_[0] & $_[1]; } sub dumpbv { my $bitv = shift @_; my $pos=0; foreach (split "",unpack("b*",$bitv)){ print; $pos++; } }

Outputs:

00011110 bv1 00111000 bv2 -------- 00111110 OR 00011000 AND

Hope this helps

citromatik


In reply to Re: oct and vec by citromatik
in thread oct and vec by pysome

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.