Searching \ for 'reversing bits' in subject line. ()
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.
'reversing bits'
1997\04\12@040003 by

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
> 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.
West Newton, MA 02165  U.S.A.

Tel: 617-244-1450        FAX: 617-964-4917
================================================
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
----------
{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
rhouripacbell.net
Raphi Houri wrote:
{Quote hidden}

He said REVERSE, which I take to be MSB1-LSB1 to LSB2-MSB2, NOT
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.
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
> rhouripacbell.net
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:

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
>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 <davidcomms.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   <dwaynerplanet.eon.net>
Trinity Electronics Systems Ltd    Edmonton, Alberta, CANADA
(403) 489-3199 voice     (403) 487-6397 fax
|
|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:
|
|  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?
> From: John Payson <supercatMCS.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:
> |
> |  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...