Your esteemed worshipnesses...

I have a 40-char hex string, and 20 40-char hex mask strings, and I need to generate 20 32-bit integer vectors.

e.g. given input

"DA39A3EE5E6B4B0D3255BFEF95601890AFD80709"
and masks 
"FFFF804020100804020100804020100804000102"
"0102FFFF80402010080402010080402010080400"
"04000102FFFF8040201008040201008040201008"
...

I need to & the input and mask_1, like so

DA39A3EE5E6B4B0D3255BFEF95601890AFD80709
FFFF804020100804020100804020100804000102
========================================
DA39804000000804020100800020100004000100

then use only the 32 bits from mask_1 that remain set in the &'ed result to generate a scalar 0<x<2**32. In this case, "DA398040..." = 3661221814.

I can do it, with a boatload of hex, substr, pack, unpack, and & use, but there's got to be a better (faster) way. I do 100 million of these at a time. I've looked at vec, but I can't figure a good way to use it.

Can anyone help me improve on this?

my $in="DA39A3EE5E6B4B0D3255BFEF95601890AFD80709"; my $mask="FFFF804020100804020100804020100804000102"; my $comb=""; for($i=0;$i<20;$i++) { $comb .= sprintf("%02X", ( hex(substr($in,$i*2,2)) & hex(substr($mas +k,$i*2,2))) ); } print "$in\n"; print "$mask\n========================================\n"; print "$comb\n"; my $bits = unpack("B*", pack("H40",$mask)); my $vecbits = unpack("B*", pack("H40",$comb)); my $vecs = ""; for ($i=0;$i<160;$i++) { if (substr($bits,$i,1) eq "1") { $vecs .= substr($vecbits,$i,1); } } my $vextor = hex(unpack("H*", pack("B32",$vecs))); print "$vextor\n";

In reply to bitmask to vector problem by dwhite20899

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.