Hi all

In a recent post, someone asked how to find if two numeric ranges overlap. I couldn't resist to try a solution based on bit vectors. That is, create 2 bit vectors with the bits inside the range set to "1", and see if the & operation on them give something different to a vector of '0's. Something like:

use strict; use warnings; my ($bv1,$bv2,$bv3) = ('') x 3; vec ($bv1,$_,1)=1 for (11..19) # First range vec ($bv2,$_,1)=1 for (15..25) # Second range vec ($bv3,19,1)=0 # The "0" vector -- should have + the length of the shorter. print unpack ("b*",$bv1),"\n"; print unpack ("b*",$bv2),"\n"; print unpack ("b*",$bv3),"\n",'-'x60,"\n"; my $olap = $bv1 & $bv2; print unpack ("b*",$olap),"\n\n"; print +(($bv1 & $bv2) eq $bv3 ? "don't overlap\n" : "overlap\n");

This works as expected:

1 000000000001111111110000 2 00000000000000011111111111000000 0s 000000000000000000000000 ------------------------------------------------------------ & 000000000000000111110000

but I find a bit tricky to have to build the '0' vector for comparisons and the fact that the bit vectors 000 and 0000000000000000 are not equals

Is there a better way to see if a bit vector has all its bits set to '0'?

Thanks in advance

citromatik


In reply to comparing bit vectors by citromatik

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.