Code archives/Algorithms/Reverse bytes and bits
This code has been declared by its author to be Public Domain code.
Download source code
| |||||
Pretty simple, it just reverse the byte and bit order. For an example: It's fun to use. | |||||
Function ReverseBytes( p@ Ptr, sz%, rbits%=0 ) Local szh% = Int(Floor(sz*.5)) sz :- 1 If rbits>0 Then For Local i:Int = 0 To szh-1 szh = p[i] ' Because szh is only used once, we can reuse it here p[i] = ReverseBits(p[sz-i]) p[sz-i] = ReverseBits(szh) Next sz :+ 1 If Int(sz*.5) <> Ceil((sz*.5)-.1) Then sz = Int(Ceil(sz*.5)) p[sz] = ReverseBits(p[sz]) EndIf Else For Local i:Int = 0 To szh-1 szh = p[i] p[i] = p[sz-i] p[sz-i] = szh Next EndIf End Function Function ReverseBitsA@( p@ ) ?Debug Return (p & %1) Shl 7.. | ((p&%10000000) Shr 7).. | (p & %10) Shl 5.. | ((p&%1000000) Shr 5).. | (p & %100) Shl 3.. | ((p&%100000) Shr 3).. | (p & %1000) Shl 1.. | ((p&%10000) Shr 1) ? Local p2:Int = ((p & %11110000) Shr 4) | ((p & %1111) Shl 4) p =((p & %11001100) Shr 2) | ((p & %110011) Shl 2) Return ((p & %10101010) Shr 1) | ((p & %1010101) Shl 1) End Function |
Comments
| ||
Function ReverseBits@( p@ ) Local p2:Int=((p & %11110000) Shr 4) | ((p & %1111) Shl 4) p=((p2 & %11001100) Shr 2) | ((p2 & %110011) Shl 2) Return ((p & %10101010) Shr 1) | ((p & %1010101) Shl 1) End Function '6 shifts 6 and's 3 or's instead of 8 , 8 and 7 - should be up to 25% faster. The only downside is having to store the interim values into a variable so you can then read in the changes to continue the operation. You can probably optimize your byte sort routine in much the same way and it should be a lot faster. It's a binary flip, so if you're flipping an Int it should only take 5 passes of swapping the sets of bits. Much better than doing it a bit at a time. I have no idea what your example program is trying to do. :-) |
| ||
For some reason that's only faster in release mode, so I'll use this:Function ReverseBitsA@( p@ ) ?Debug Return (p & %1) Shl 7.. | ((p&%10000000) Shr 7).. | (p & %10) Shl 5.. | ((p&%1000000) Shr 5).. | (p & %100) Shl 3.. | ((p&%100000) Shr 3).. | (p & %1000) Shl 1.. | ((p&%10000) Shr 1) ? Local p2:Int = ((p & %11110000) Shr 4) | ((p & %1111) Shl 4) p =((p & %11001100) Shr 2) | ((p & %110011) Shl 2) Return ((p & %10101010) Shr 1) | ((p & %1010101) Shl 1) End Function Bit bloated, but it's fast in both situations. Example program is just trying to show that it's fun to use. It's pretty useless for a lot of things, but it's fun to use it for completely bizarre things too. |
Code Archives Forum