savangadi has asked for the wisdom of the Perl Monks concerning the following question:

I want to calculate the 7-bit ECC for 32 bit data. The data string I have can be either in hex (e.g. 3E10F67A) or binary format. The 7-bit parity is fixed. Can you give me an example of doing this efficiently? Thanks!

Replies are listed 'Best First'.
Re: ECC computation
by LanX (Saint) on Jan 04, 2020 at 23:46 UTC
Re: ECC computation
by Anonymous Monk on Jan 04, 2020 at 23:32 UTC
    "the" 7-bit ECC for 32 bit data?? which one exactly? algorithm? example calculations?
      The parity scheme is as follows:
      P0 = D0 + D1 + D3 + D4 + D6 + D8 + D10 + D11 + D13 + D15 + D17 + D19 + + D21 + D23 + D25 + D26 + D28 + D30 P1 = D0 + D2 + D3 + D5 + D6 + D9 + D10 + D12 + D13 + D16 + D17 + D20 + + D21 + D24 + D25 + D27 + D28 + D31 P2 = D1 + D2 + D3 + D7 + D8 + D9 + D10 + D14 + D15 + D16 + D17 + D22 + + D23 + D24 + D25 + D29 + D30 + D31 P3 = D4 + D5 + D6 + D7 + D8 + D9 + D10 + D18 + D19 + D20 + D21 + D22 + + D23 + D24 + D25 P4 = D11 + D12 + D13 + D14 + D15 + D16 + D17 + D18 + D19 + D20 + D21 + + D22 + D23 + D24 + D25 P5 = D26 + D27 + D28 + D29 + D30 + D31 P6 = D0 + D1 + D2 + D3 + D4 + D5 + D6 + D7 + D8 + D9 + D10 + D11 + D12 + + D13 + D14 + D15 + D16 + D17 + D18 + D19 + D20 + D21 + D22 + D23 + +D24 + D25 + D26 + D27 + D28 + D29 + D30 + D31 + P0 + P1 + P2 + P3 + P +4 + P5
        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]