in reply to Re^2: ECC computation
in thread ECC computation

This returns 1010001 for your sample input. Is it correct? Is it efficient enough?
#!/usr/bin/perl use warnings; use strict; use List::Util qw{ sum }; sub ecc { my ($hex) = @_; my $binary = pack 'H8', $hex; my @d = split //, unpack 'B32', $binary; my @p = map sum(map 0 + $d[$_], @$_) % 2, [ 0, 1, 3, 4, 6, 8, 10, 11, 13, 15, 17, 19, 21, 23, 25, 26, 28 +, 30 ], [ 0, 2, 3, 5, 6, 9, 10, 12, 13, 16, 17, 20, 21, 24, 25, 27, 28 +, 31 ], [ 1, 2, 3, 7, 8, 9, 10, 14, 15, 16, 17, 22, 23, 24, 25, 29, 30 +, 31 ], [ 4 .. 10, 18 .. 25 ], [ 11 .. 25 ], [ 26 .. 31 ], [ 0 .. 31 ]; $p[6] += sum(@p[ 0 .. 5 ]); $p[6] %= 2; return join "", @p } my $hex = '3E10F67A'; print ecc($hex);

Update: Added the 0 + which seems to speed up the sub by more than 60%. In a previous update, I used % 2 instead, which caused a speed-up of about 50%.

Update 2: fixed B64 to B32 (thanks LanX).

Update 3: fixed a bug: $p[6] must be % 2 separately from +=, otherwise it can get greater than 1.

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Replies are listed 'Best First'.
Re^4: ECC computation
by LanX (Saint) on Jan 05, 2020 at 03:03 UTC
    If you want speed this up, you might wanna try unpack's template for counting bits.

    perlpacktut#Doing-Sums

    And an even parity bit can be determined like this:

    my $evenparity = unpack( '%1b*', $mask );

    And you could use & to isolate the right bits with pre-calculated bit-masks.

    PS: not sure why you use B64 for 32bit hex.

    update

    that's a weak demo because I don't seem to get MSB and LSB right.

    DB<85> $n=pack 'h8', '3E10F67A' DB<86> undef $m0; vec($m0,$_,1)=1 for 0, 1, 3, 4, 6, 8, 10, 11, 13, + 15, 17, 19, 21, 23, 25, 26, 28, 30 DB<87> say unpack 'b32',$_ for $n, $m0, $n & $m0 11000111100000001111011011100101 11011010101101010101010101101010 11000010100000000101010001100000 DB<88> p unpack '%1b', $n & $m0 1

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      Yes, I'm not a frequent user of pack and unpack so I often forget what tricks they can do. So, here's the full solution, more than five times faster than my previous one:
      my @masks = qw( 11011010101101010101010101101010 10110110011011001100110011011001 01110001111000111100001111000111 00001111111000000011111111000000 00000000000111111111111111000000 00000000000000000000000000111111 11111111111111111111111111111111 ); sub ecc { my ($hex) = @_; my $d = unpack 'B32', pack 'H8', $hex; my @r; push @r, unpack '%1B*', $_ & $d for @masks; $r[-1] = unpack '%1B*', $r[-1] . join "", @r[ 0 .. $#r - 1 ]; return join "", @r }
      map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
        > more than five times faster than my previous one:

        Cool, it works?

        > Yes, I'm not a frequent user of pack and unpack

        neither am I, so I left it to you ... ;-)

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery FootballPerl is like chess, only without the dice