Its all at http://www.piclist.com under routines, math, bit operations, reversing bit order in a byte --- James Newton mailto:jamesnewton@geocities.com 1-619-652-0593 http://techref.massmind.org NEW! FINALLY A REAL NAME! Members can add private/public comments/pages ($0 TANSTAAFL web hosting) -----Original Message----- From: pic microcontroller discussion list [mailto:PICLIST@MITVMA.MIT.EDU]On Behalf Of Scott Dattalo Sent: Sunday, March 19, 2000 06:14 To: PICLIST@MITVMA.MIT.EDU Subject: Re: Reversing the order of bits in a register. On Sun, 19 Mar 2000, Nikolai Golovchenko wrote: > cblock > In > Out > endc > > #define A_MASK 1 > #define B_MASK 2 > #define C_MASK 4 > #define D_MASK 8 > #define E_MASK 16 > #define F_MASK 32 > #define G_MASK 64 > #define H_MASK 128 > > clrw > btfsc In, 7 > iorlw A_MASK > btfsc In, 6 > iorlw B_MASK > btfsc In, 5 > iorlw C_MASK > btfsc In, 4 > iorlw D_MASK > btfsc In, 3 > iorlw E_MASK > btfsc In, 2 > iorlw F_MASK > btfsc In, 1 > iorlw G_MASK > btfsc In, 0 > iorlw H_MASK > movwf Out For a single byte there is a 14-cycle solution. But since it looks like your having fun, I wouldn't want to spoil anything. :) BTW, John Payson posted the original solution (for reversing 7 bits). Dmitry optimized it slightly (again for 7 bits). John's trick is applicable here as well. Hint, for the general case, it takes 4 cycles to swap 2 bits. However, it only takes 2 cycles for the first two bits. Scott