Egads! As I was typing up that message, there was a fresh entry from Dmitriy -- very slick solution, that now takes the top spot with the fewest # of steps. I haven't actually tried it, but a mental walk-thru tells me it should work. And it actually uses a neat XOR swap technique. My hat's off to you sir. Cheers, -Neil. ----- Original Message ----- From: "Pic Dude" To: Sent: Sunday, March 10, 2002 12:01 AM Subject: [PIC}: Re: Bit operation quiz... > Ack! forgot the [PIC]: prefix. Anyway, been looking thru the > code snippets that were posted, and my resulting thought is that > I need to stop making life so complicated on myself. In my > past life, when I coded assembly on the TRS-80, I remember > something about conditionals being very expensive, so I tend > to avoid them. Gotta get into the mindset that things are not > the same anymore. As Bob Ammerman demonstrated, the > solution with the fewest steps, was the most brute force method > possible -- to test each of the 4 lowest bits and set the > corresponding bit in the result. Only 11 steps! > > That said, I must say that I like the way Tom Messenger thinks > -- really slick dataflow. Sequentially squeeze the 4 lowest bits > out the right and push it into the result from the right (to the left). > Tom, as for the other solutions, I am actually running out of > space on the F872. I'm sure however, that I can do a lot of > cleanup on my code -- this is still my first PIC code ever. As > for wiring one port into the other -- nice thought, but I started > this bit-reversal thing to clean up wires, and the other ports are > in use. > > Jinx, your solution had the top 4 bits swapped. With some > code adjustments I managed to get the result in 14 steps. > > Nigel unfortunately I'm not using C, but your code flow is > pretty much the same as Bob's -- check each bit and set the > output bits accordingly. > > This was fun! Must do this again sometime! :-) > > Thanks much guys! > -Neil. > > > > ----- Original Message ----- > From: "Pic Dude" > To: > Sent: Friday, March 08, 2002 9:29 PM > Subject: Bit operation quiz... > > > Anyone up for a pop quiz?... > > I have all Port C outputs going to 8 LED's. To save PCB space, I can > re-route > the 4 lowest lines to the LED's, but the last 4 bits get reversed. What's > the fastest > way, in code, to reverse just those 4 lowest bits, and leave the 4 highest > bits > untouched? Fastest = smallest # of steps. > > Assume input = ABCDEFGH, then output should be ABCDHGFE. > > So far I've come up with this (untested), but it's seems like a very > "sledgehammer" way to do it. There's gotta be something slicker. I > remember learning about the XOR swap trick many years ago, and > thought it was the coolest thing ever (at the time). Any thing like > that exist for doing this partial reverse? > > > movf DATAIN,TEMP > rrf TEMP,F > movf TEMP,W > andlw H'02' > movwf DATAOUT ; Bit 1 done > rrf TEMP,F > rrf TEMP,W > andlw H'01' > andwf DATAOUT,F ; Bit 0 done > movf DATAIN,TEMP > rlf TEMP,F > movf TEMP,W > andlw H'04' > andwf DATAOUT,F ; Bit 2 done > rlf TEMP,F > rlf TEMP,W > andlw H'08' > andwf DATAOUT,F ; Bit 3 done > movf DATAIN,W > andlw H'F0' ; Strip out low nibble > andwf DATAOUT,F ; Merge with result > > > Cheers, > -Neil. > > -- > http://www.piclist.com hint: PICList Posts must start with ONE topic: > [PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads > > -- > http://www.piclist.com hint: The PICList is archived three different > ways. See http://www.piclist.com/#archives for details. > > > -- http://www.piclist.com hint: The PICList is archived three different ways. See http://www.piclist.com/#archives for details.