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..
> From: Alan King <spam_OUTtakingTakeThisOuTintrex.net>
> To: .....PICLISTKILLspam@spam@MITVMA.MIT.EDU
> Subject: Re: reversing bits
> Date: Saturday, April 12, 1997 1:40 AM
>
> 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
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:
>
> ----------
> > From: Alan King <.....takingKILLspam.....intrex.net>
> > To: EraseMEPICLISTspam_OUTTakeThisOuTMITVMA.MIT.EDU
> > Subject: Re: reversing bits
> > Date: Saturday, April 12, 1997 1:40 AM
> >
> > 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
>
> 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
> rhourispam_OUTpacbell.net
He said REVERSE, which I take to be MSB1-LSB1 to LSB2-MSB2, NOT
invert. Reading is basic.
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.
>
> ----------
> > From: Alan King <@spam@takingKILLspamintrex.net>
> > To: KILLspamPICLISTKILLspamMITVMA.MIT.EDU
> > Subject: Re: reversing bits
> > Date: Saturday, April 12, 1997 1:40 AM
> >
> > 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
>
> 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!
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
> RemoveMErhouriTakeThisOuTpacbell.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.
> From: David Tait <spamBeGonedavidspamBeGonecomms.ee.man.ac.uk>
> [cut]
> CLRF REVERSE ; Precondition Register to 0
> BTFSC NORMAL, 7 ; MSb set?
> BSF REVERSE, 0 ; Set LSb
> BTFSC NORMAL, 6 ; bit set?
> BSF REVERSE, 1 ; Set bit
> BTFSC NORMAL, 5 ; bit set?
> BSF REVERSE, 2 ; Set bit
> BTFSC NORMAL, 4 ; bit set?
> BSF REVERSE, 3 ; Set bit
> BTFSC NORMAL, 3 ; bit set?
> BSF REVERSE, 4 ; Set bit
> BTFSC NORMAL, 2 ; bit set?
> BSF REVERSE, 5 ; Set bit
> BTFSC NORMAL, 1 ; bit set?
> BSF REVERSE, 6 ; Set bit
> BTFSC NORMAL, 0 ; bit set?
> BSF REVERSE, 7 ; Set bit
>
> But then again this might be too obvious for the PICLIST ...
>
> David
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
>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 <TakeThisOuTdavidEraseMEspam_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.
|
|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?
> From: John Payson <supercatEraseME.....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).