Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!uunet!zaphod.mps.ohio-state.edu!uwm.edu!ogicse!iWarp.intel.com!news From: merlyn@iWarp.intel.com (Randal L. Schwartz) Newsgroups: comp.lang.perl Subject: Re: flipping bits in perl Message-ID: <1991May31.162709.7069@iWarp.intel.com> Date: 31 May 91 16:27:09 GMT References: <1991May30.231900.12417@com50.c2s.mn.org> Sender: news@iWarp.intel.com Reply-To: merlyn@iWarp.intel.com (Randal L. Schwartz) Organization: Stonehenge; netaccess via Intel, Beaverton, Oregon, USA Lines: 35 In-Reply-To: caa@com50.c2s.mn.org (Charles Anderson) Nntp-Posting-Host: se.iwarp.intel.com In article <1991May30.231900.12417@com50.c2s.mn.org>, caa@com50 (Charles Anderson) writes: | | Yesterday a coworker asked me if I could flip all of the bits in the | font file he was using, and I decided to try using perl to do it. | (I could easily do it in C but I'm trying to learn perl and thought | I'd give it a go.) I tried all sorts of things tr/\000-\377/\377-\000/ | didn't work neither did s/./~$&/g in various type of things. I even | tried unpacking it into an array and flipping from there but that wasn't | very succesful either. I but a vec() into the code (something like | vec("blah", 0, 1) to see if the bit twiddling would start working after | that but it didn't. I'm stumped...anybody know how to do it. All I | really want is a bit negation or xor $ff. Here's a really awfully fast program hand-tuned by Larry and me while we were one-upping each other while writing the book. It does exactly what you asked for. *How* it does it should be a useful puzzle to figure out... #!/usr/bin/perl $bufsize = 16384; vec($ones,0,8) = 255; $ones x= $bufsize; while(read(STDIN,$_,$bufsize)) { print STDOUT length != $bufsize ? substr($_ ^ $ones, 0, length) : $_ ^ $ones; } or, you can try it like this: print pack("C*",grep(($_^=255)||1,unpack("C*",pack("H*","b58a8c8bdf9e91908b979a8ddfaf9a8d93df979e9c949a8dd3")))) -- /=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\ | on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III | | merlyn@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn | \=Cute Quote: "Intel: putting the 'backward' in 'backward compatible'..."====/