Searching \ for 'reversing bits' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/index.htm?key=reversing+bits
Search entire site for: 'reversing bits'.

Truncated match.
PICList Thread
'reversing bits'
1997\04\12@040003 by Tony Matthews

flavicon
face
Is there a simple (short) way to reverse the order of bits in a byte.
or even better a nibble in the middle of a byte ?       Thanks

1997\04\12@041706 by wfdavis

flavicon
face
> Is there a simple (short) way to reverse the order of bits in a byte.
> or even better a nibble in the middle of a byte ?       Thanks

Yes.  The simplest, and also fastest, is by table lookup.

--- Warren Davis
================================================
Davis Associates, Inc.
43 Holden Road
West Newton, MA 02165  U.S.A.

Tel: 617-244-1450        FAX: 617-964-4917
Visit our web site at:  http://www.davis-inc.com
================================================

1997\04\12@043813 by taking

flavicon
face
Tony Matthews wrote:
>
> Is there a simple (short) way to reverse the order of bits in a byte.
> or even better a nibble in the middle of a byte ?       Thanks


 Clear carry,
 Rotate left out of in byte (or right)
 Rotate right into out byte (or left)
 Repeat 8 times.  What went out of first byte goes into other side
  of second.  Actually, you don't have to clear the carry even..

By the time you get done with it, the 16 inline instructions to rotl,
rotr,rotl,rotr, & etc won't be much more than setting up a counter and
looping 8 times, and won't use ram or destroy anything..

Alan

1997\04\12@155623 by Raphi Houri

flavicon
face
----------
{Quote hidden}

Let's get back to basics

exclusive OR truth table:


       a   b   f(a,b)
           -----------------
       0   0 |   0
       0  1  |   1
       1  0  |   1
       1  1  |   0

From the table, if you EOR a byte with all 1s you will get all the bytes
inverted.
You may also invert selected bits.
example code:

       (W=%11001001) EOR (%11111111) = %00110110

       16c84 code:
               XORLW    0xff

Boolean algebra is beautiful!

Raphi Houri
rhourispamKILLspampacbell.net

1997\04\12@161542 by taking
flavicon
face
Raphi Houri wrote:
{Quote hidden}

 He said REVERSE, which I take to be MSB1-LSB1 to LSB2-MSB2, NOT
invert.  Reading is basic.

1997\04\13@112344 by Ed Todd

picon face
I use bit reversal a fair bit.  For example, if I am debugging my timing, I
might toggle a port at a critical time and use a logic analyzer.  Here is
the way I do it:

       MOVLW   B'00001000'
       XORWF   PORTB,F

If PORTB is 00011100 and you execute this code, it becomes 00010100.
Execute it again, you're back at 00011100.  Same works for any register.
To toggle lower nybble, use MOVLW h'0f' and XORWF the register.

1997\04\13@235855 by Tony Matthews

flavicon
face
Raphi Houri wrote:
{Quote hidden}

Agreed but I wanted to reverse the order of bits not invert them and I
have gotten several good ideas thanks to all who replied. Tony M.
> Raphi Houri
> RemoveMErhouriTakeThisOuTspampacbell.net

1997\04\14@112717 by sdattalo

face
flavicon
face
Tony Matthews wrote:
>
> Is there a simple (short) way to reverse the order of bits in a byte.
> or even better a nibble in the middle of a byte ?       Thanks


Here's something David Tait and Steve Hardy posted almost a
year ago.

{Quote hidden}

Yes, it's too obvious.  Shave one cycle with:

 rrf  NORMAL
 rlf  REVERSE
 ... (repeat 8 times total)

Unfortunately, NORMAL is destroyed in the process.  However, we can turn
this to our advantage if we want to bit reverse TWO registers at once:
just add

 rlf  REVERSE,w

at the beginning.  Both NORMAL and REVERSE will be swapped and reversed
at the expense of W which is now destroyed (althouh hopefully not
irreparably).

Regards,
SJH
Canberra, Australia


Two excellent solutions to which I have nothing else to add.

Scott
--
"The problem with television is not the resolution."
                                Hugh. F. Frohbach

1997\04\14@131956 by Dwayne Reid

flavicon
face
>Tony Matthews wrote:
>>
>> Is there a simple (short) way to reverse the order of bits in a byte.
>> or even better a nibble in the middle of a byte ?       Thanks
>
>
>Here's something David Tait and Steve Hardy posted almost a
>year ago.
>
>> From: David Tait <TakeThisOuTdavidEraseMEspamspam_OUTcomms.ee.man.ac.uk>
>> [cut]

I have been following the discussion on reversing the order of bits in a
byte and wonder if the following has been suggested:

   clrw
   btfsc   NORMAL,7
     iorlw b'00000001'
   btfsc   NORMAL,6
     iorlw b'00000010'
   btfsc   NORMAL,5
     iorlw b'00000100'
   btfsc   NORMAL,4
     iorlw b'00001000'
   btfsc   NORMAL,3
     iorlw b'00010000'
   btfsc   NORMAL,2
     iorlw b'00100000'
   btfsc   NORMAL,1
     iorlw b'01000000'
   btfsc   NORMAL,0
     iorlw b'10000000'
   movwf   NORMAL          ;save reversed byte on top of original

It takes 2 more instructions than other methods mentioned but saves 1 RAM.
On the smaller parts, this may be a worthwile tradeoff.


Dwayne Reid   <RemoveMEdwaynerspamTakeThisOuTplanet.eon.net>
Trinity Electronics Systems Ltd    Edmonton, Alberta, CANADA
(403) 489-3199 voice     (403) 487-6397 fax

1997\04\14@200100 by John Payson

picon face
|
|Yes, it's too obvious.  Shave one cycle with:
|
|  rrf  NORMAL
|  rlf  REVERSE
|  ... (repeat 8 times total)
|
|Unfortunately, NORMAL is destroyed in the process.  However, we can turn
|this to our advantage if we want to bit reverse TWO registers at once:
|just add
|
|  rlf  REVERSE,w
|
|at the beginning.  Both NORMAL and REVERSE will be swapped and reversed
|at the expense of W which is now destroyed (althouh hopefully not
|irreparably).

Why not instead add "rlf NORMAL,f" to the end?  This would [1] not trash W, and
[2] also preserve/restore the carry flag?

1997\04\14@201738 by Steve Hardy

flavicon
face
> From: John Payson <supercatEraseMEspam.....MCS.NET>
>
> |
> |Yes, it's too obvious.  Shave one cycle with:
> |
> |  rrf  NORMAL
> |  rlf  REVERSE
> |  ... (repeat 8 times total)
> |
> |Unfortunately, NORMAL is destroyed in the process.  However, we can turn
> |this to our advantage if we want to bit reverse TWO registers at once:
> |just add
> |
> |  rlf  REVERSE,w
> |
> |at the beginning.  Both NORMAL and REVERSE will be swapped and reversed
> |at the expense of W which is now destroyed (althouh hopefully not
> |irreparably).
>
> Why not instead add "rlf NORMAL,f" to the end?  This would [1] not trash W,
and
> [2] also preserve/restore the carry flag?
>

Dammit, why didn't I think of that?  (Except it should be rrf normal at the
end, not rlf).

Regards,
SJH
CAnberra, Australia

More... (looser matching)
- Last day of these posts
- In 1997 , 1998 only
- Today
- New search...