Path: utzoo!news-server.csri.toronto.edu!cs.utexas.edu!uwm.edu!spool.mu.edu!uunet!paralogics!shaw From: shaw@paralogics.UUCP (Guy Shaw) Newsgroups: comp.lang.perl Subject: Re: Even Parity Table Generation Summary: Use reflected binary Gray codes Keywords: reflected binary codes, gray codes, parity Message-ID: <426@paralogics.UUCP> Date: 12 Mar 91 11:56:21 GMT References: <121817@uunet.UU.NET> Organization: Paralogics; Santa Monica, CA Lines: 42 One non-recursive way to generate a parity table is to count through the character set in (binary reflected) Gray code order. Since the successor of any Gray code number changes only one binary digit, the parity will flip back and forth between even and odd. #! /usr/local/bin/perl # For purposes of demonstration, # print out the first 32 Gray code numbers print "bin gray\n"; print "--- ---\n"; for $i (0..31) { printf "%3x %3x\n", $i,$i^($i>>1); } print "\n"; &MakeEvenParityTable; # Generate full 256-entry even parity table, # but just print the first 32 for $i (0..31) { printf "%3x %3x\n", $i,$even[$i]; } exit 0; sub MakeEvenParityTable { local($bin,$gray,$parity); $even[0] = 0; $parity = 0; for $bin (1..255) { $gray = $bin ^ ($bin>>1); $parity = $parity ^ 128; $even[$gray] = $gray | $parity; } } -- Guy Shaw Paralogics paralogics!shaw@uunet.uu.net or uunet!paralogics!shaw