Code: Select all

Function ReverseBits32(x As ULong) As ULong

x = ((x And &haaaaaaaaull) Shr 1) Or ((x And &h55555555ull) Shl 1)

x = ((x And &hccccccccull) Shr 2) Or ((x And &h33333333ull) Shl 2)

x = ((x And &hf0f0f0f0ull) Shr 4) Or ((x And &h0f0f0f0full) Shl 4)

x = ((x And &hff00ff00ull) Shr 8) Or ((x And &h00ff00ffull) Shl 8)

Return (x Shr 16) or (x Shl 16)

End Function

Function Reversebits64(x As ULongInt) As ULongInt

x = ((x And &haaaaaaaaaaaaaaaaull) Shr 1) Or ((x And &h5555555555555555ull) Shl 1)

x = ((x And &hccccccccccccccccull) Shr 2) Or ((x And &h3333333333333333ull) Shl 2)

x = ((x And &hf0f0f0f0f0f0f0f0ull) Shr 4) Or ((x And &h0f0f0f0f0f0f0f0full) Shl 4)

x = ((x And &hff00ff00ff00ff00ull) Shr 8) Or ((x And &h00ff00ff00ff00ffull) Shl 8)

x = ((x And &hffff0000ffff0000ull) Shr 16) Or ((x And &h0000ffff0000ffffull) Shl 16)

Return (x Shr 32) or (x Shl 32)

End Function

Dim As ULongInt x = 100

Print "X = "; x

Print Bin(x, 64)

print

Print "Now in reversed order."

x = Reversebits64(x)

Print

Print "X = "; x

Print Bin(x, 64)

Sleep