Re: Bit-order reversal (little-endian <--> big-endian)

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



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
>


.



Relevant Pages

  • Re: lang effort: type conversions
    ... is that your entire type system? ... JavaScripts variables give a good model for a variant ... if you have special syntax just to convert between built-in ... whats wrong with explicit conversions between inbuilt types as ...
    (comp.lang.misc)
  • Re: lang effort: type conversions
    ... is that your entire type system? ... JavaScripts variables give a good model for a variant type, ... whats wrong with explicit conversions between inbuilt types as ...
    (comp.lang.misc)
  • Re: Invalid variant type conversion
    ... >> The record field is a character value. ... >determine possible conversions within the TField type, ... I'm no expert on Tables - in Delphi or VB ... had problems with 'Variant oriented' Databases ...
    (comp.lang.pascal.delphi.misc)
  • Re: ADO vs ADOExpress time trials. Not good for ADOExpress
    ... Additionally, that returns a variant. ... But considering it is 3x faster to do the variant conversions yourself, ... let TField do it - i might be forcing people here to learn the syntax. ...
    (borland.public.delphi.database.ado)