Re: Bit-order reversal (little-endian <--> big-endian)
- From: "David J Mark" <nntp45534-22@xxxxxxxxxxxx>
- Date: Fri, 30 Dec 2005 10:43:28 GMT
I benchmarked your solution against mine. The results are completely
inconclusive. Identical loops with identical data yielded seemingly random
results. There was no clear winner. I then changed the mod and optimized
the last line to prevent conversions and there was little difference (made
mine win a little more often.) One thing is certain: mine doesn't loop
extra times. For random data, this will make it faster (even without the
extra optimizations.)
The optimized version of mine:
Dim lResult As Long
Dim lCount As Long
While lX > 0
lResult = lResult * 2
lResult = lResult Or (lX And 1)
lX = lX \ 2
lCount = lCount + 1
Wend
ReverseBits8 = lResult * 2& ^ (IIf(Nibble, 4&, 8&) - lCount)
"Michael C" <nospam@xxxxxxxxxx> wrote in message
news:%23vHMe7RDGHA.412@xxxxxxxxxxxxxxxxxxxxxxx
> "David J Mark" <nntp45534-22@xxxxxxxxxxxx> wrote in message
> news:CB5tf.91828$lh.82534@xxxxxxxxxxxxxxxxxxxxxxxxx
>> As in the bits are in reverse order? Not the bytes apparently. And you
>> need an option for nibbles apparently.
>
> This code is going to be horribly inefficient for what should be a few
> lines of assembly code.
>
>> Public Function ReverseBits8(ByVal lX As Long, Optional Nibble = False)
>> As
>
>> lResult = lResult + (lX Mod 2)
>
> Mod is a division when an And will do it much more efficiently.
>
>> lX = lX \ 2
>
> Integer divide is good.
>
>> ReverseBits8 = lResult * 2 ^ (IIf(Nibble, 4, 8) - iCount)
>
> But this is mixing all sorts of conversions in here from long to double,
> integer to variant, variant to double, long to double and double to long.
>
>> If (lX Mod 2) Then lResult = lResult + 1
>
> Could just use lResult = lResult or (IX and 1)
>
>> lResult = lResult * 2 ^ (31 - iCount)
>
> This has a good mix of conversions also except without the variants.
>
> I'd say it would be possible to write the function in 10 lines of assembly
> code. If done well in VB 20 lines would be generated but what you've
> written would top 200. In addition it would call thousands of lines of
> code to convert variants, calculate powers etc.
>
> Michael
>
.
- Follow-Ups:
- Re: Bit-order reversal (little-endian <--> big-endian)
- From: Michael C
- Re: Bit-order reversal (little-endian <--> big-endian)
- References:
- Bit-order reversal (little-endian <--> big-endian)
- From: Bob O`Bob
- Re: Bit-order reversal (little-endian <--> big-endian)
- From: Rick Rothstein [MVP - Visual Basic]
- Re: Bit-order reversal (little-endian <--> big-endian)
- From: Bob O`Bob
- Re: Bit-order reversal (little-endian <--> big-endian)
- From: David J Mark
- Re: Bit-order reversal (little-endian <--> big-endian)
- From: Michael C
- Bit-order reversal (little-endian <--> big-endian)
- Prev by Date: Re: ADO objects like Connection and Recordset ETC.
- Next by Date: Re: Detect idle time
- Previous by thread: Re: Bit-order reversal (little-endian <--> big-endian)
- Next by thread: Re: Bit-order reversal (little-endian <--> big-endian)
- Index(es):
Relevant Pages
|