Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!elroy.jpl.nasa.gov!jpl-devvax!lwall From: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Newsgroups: comp.lang.perl Subject: Re: Even Parity Table Generation Message-ID: <11387@jpl-devvax.JPL.NASA.GOV> Date: 11 Feb 91 18:51:38 GMT References: <121817@uunet.UU.NET> Reply-To: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Organization: Jet Propulsion Laboratory, Pasadena, CA Lines: 33 In article <121817@uunet.UU.NET> rbj@uunet.UU.NET (Root Boy Jim) writes: : I needed an even parity table, so I decided to generate it recursively. : I thought people might be interested in my solution, and or : commenting on how it might be done better. And while I'm at it, : I hereby request another function: chr(), the opposite of ord(). : Either that, or get rid of ord. How 'bout: sub chr {sprintf("%c",$_[0]);} : Anyway, here it is: [recursive solution omitted] I'd just say: #!/usr/bin/perl for (0 .. 127) { $bits = unpack('B8',pack('C',$_)); printf "%x %x\n", $_, $_ | ((($bits =~ tr/1/1/) & 1) << 7); } or maybe for (0 .. 127) { $bits = unpack(B8,pack(C,$_)); $bits =~ s/0/1/ if $bits =~ tr/1// & 1; printf "%x %x\n", $_, unpack(C,pack(B8, $bits)); } (Requires 3.0 patchlevel 44.) Larry