dwhite20899 has asked for the wisdom of the Perl Monks concerning the following question:
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";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: bitmask to vector problem
by BrowserUk (Patriarch) on Jan 06, 2005 at 20:27 UTC | |
|
Re: bitmask to vector problem
by sgifford (Prior) on Jan 06, 2005 at 20:25 UTC | |
|
Re: bitmask to vector problem
by dwhite20899 (Friar) on Jan 14, 2005 at 03:41 UTC |