piclist 1999\10\29\185302a >
Thread: Efficient way to reverse a byte?
picon face BY : Russell McMahon email (remove spam text)

The piece of code that Tracy posted (from John Payson) does what it was
originally meant to do BUT is NOT correct for the present problem.

Here we require
76543210 --> 01234567

John's solution is for a 7 bit swap ignoring B7.
ie x6543210 --> x0123456

As a consequence B7 & B3 is untouched and there are only 3 swaps needed
making the code shorter.
Rewriting it for an 8 bit swap as required here, produces -

       movwf   source

       btfsc   source,0
        xorlw  h'81'
       btfsc   source,7
        xorlw  h'81'
       btfsc   source,1
        xorlw  h'42'
       btfsc   source,6
        xorlw  h'42'
       btfsc   source,2
        xorlw  h'24'
       btfsc   source,5
        xorlw  h'24'
       btfsc   source,3
        xorlw  h'18'
       btfsc   source,4
        xorlw  h'18'

17 instructions, time varies depending on data but fairly quick.

Operation may not be initially obvious.
It implements
   For N = 0 to 3
       If bN = 1 then invert bits bN and b(7-N)
       If b(7-N) = 1 then invert bits bN and b(7-N)
   Next N

Worst case, if both bits in a pair are set it inverts them both twice.
Best case, if neither is set it takes no action.
This suggests that a more cunning test for "both bits set" may allow a small
reduction in run time but intuition suggests that the testing overhead would
be excessive..

John's solution is elegant but a  little hard on the brain.
I prefer Dwayne Reids more brute force -

{Quote hidden}

because it's much easier for trhe average person to see what is happening at
a glance (or a few glances :-) - YMMV).

A full 8 bit lookup table is much quicker and much much less code space

A dual nibble lookup table is not much quicker on average and rather less
code efficient.

Lookup tables have the possible advantage of having a constant
implementation time.

     Russell McMahon

>From another world - http://www.easttimor.com

What can one man* do?
Help the hungry at no cost to yourself!
at  http://www.thehungersite.com/

(* - or woman, child or internet enabled intelligent entity :-))

From: Tracy Smith <KILLspamdot_lo.....spamspamBeGoneYAHOO.COM>

{Quote hidden}

<019701bf225c$eaf31cc0$533761cb@mkbs> 7bit

See also: www.piclist.com/techref/index.htm?key=efficient+way+reverse
Reply You must be a member of the piclist mailing list (not only a www.piclist.com member) to post to the piclist. This form requires JavaScript and a browser/email client that can handle form mailto: posts.
Subject (change) Efficient way to reverse a byte?

month overview.

new search...