Reverse Bits

Post your FreeBASIC tips and tricks here. Please don’t post your code without including an explanation.
ShawnLG
Posts: 136
Joined: Dec 25, 2008 20:21

Reverse Bits

Postby ShawnLG » Jul 30, 2020 4:42

Need bits reversed? Theres a clever bit hack for that.

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
jj2007
Posts: 1523
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Reverse Bits

Postby jj2007 » Jul 30, 2020 8:42

deltarho[1859]
Posts: 2530
Joined: Jan 02, 2017 0:34
Location: UK

Re: Reverse Bits

Postby deltarho[1859] » Jul 30, 2020 11:12

Off topic a bit, err - a byte I should say.

Code: Select all

Dim As Ulong x = &H01234567

Asm
  mov eax, Dword Ptr[x]
  bswap eax
  mov Dword Ptr[x], eax
End Asm
Print Hex(x)
Sleep

gives 67452301

Obviously, big-endian to little-endian and vice versa but I have used it in some very strange places.

Eg

Code: Select all

rdtsc
bswap eax

and you have got yourself a 32-bit seed.
jj2007
Posts: 1523
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Reverse Bits

Postby jj2007 » Jul 30, 2020 11:17

David, that's much more than a bit off topic! 8 times as much!
deltarho[1859]
Posts: 2530
Joined: Jan 02, 2017 0:34
Location: UK

Re: Reverse Bits

Postby deltarho[1859] » Jul 30, 2020 11:31

jj2007 wrote:8 times as much!

Granted, although I would say only 2^3 as much. Image

I look forward to being scolded by admin. Image
jj2007
Posts: 1523
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Reverse Bits

Postby jj2007 » Jul 30, 2020 12:37

deltarho[1859] wrote:I look forward to being scolded by admin. Image
Me too Image. Although it would make more sense scolding the OP for not crediting the source ;-)
deltarho[1859]
Posts: 2530
Joined: Jan 02, 2017 0:34
Location: UK

Re: Reverse Bits

Postby deltarho[1859] » Jul 30, 2020 16:42

jj2007 wrote:Although it would make more sense scolding the OP for not crediting the source ;-)

I couldn't possibly comment - I don't get involved in controversy - as is well known. Image
dodicat
Posts: 6559
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Reverse Bits

Postby dodicat » Jul 30, 2020 20:30

This one is one as much with a phase shift.

Code: Select all

screen 17

sub gnirtSwarD(s as string,x as long=0,y as long=0)
#define map(a,b,x,c,d) ((d)-(c))*((x)-(a))/((b)-(a))+(c)
var e=8*len(s)
dim as any ptr im(1)={imagecreate(e,16),imagecreate(e,16)}
draw string im(0),(1,0),s
for x as long=0 to e
    for y as long=0 to 16
        if point(x,y,im(0))=15 then
        var x1=map(0,e,x,e,0)
        pset im(1),(x1,y)
        end if
    next
next
put(x,y),im(1),pset
imagedestroy Im(0)
imagedestroy Im(1)
end sub

Dim As ULongInt x = 100


Print Bin(x, 64)
print
Print "Now in reversed order."
print
gnirtSwarD(Bin(x, 64),0,80)

Sleep

 
ShawnLG
Posts: 136
Joined: Dec 25, 2008 20:21

Re: Reverse Bits

Postby ShawnLG » Jul 30, 2020 22:08

jj2007 wrote:
deltarho[1859] wrote:I look forward to being scolded by admin. Image
Me too Image. Although it would make more sense scolding the OP for not crediting the source ;-)


I found the example here. https://aticleworld.com/5-way-to-reverse-bits-of-an-integer/

Doticat, Nice way to do it but the "bits" are not put back into x. you gave me an idea on using strings.

Code: Select all

Dim As ULongInt x1 = 100, x2
Dim As String s1
s1 = Bin(x1, 64)

Dim As UByte Ptr sptr = StrPtr(s1)
For i As Integer = 0 To 31
   Swap sptr[i], sptr[63-i]' swap the "bits" into reverse order.
Next i

x2 = Vallng("&b"+s1)

Print Bin(x1, 64)
print
Print "Now in reversed order."
Print
Print Bin(x2, 64)


sleep

MrSwiss
Posts: 3542
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Reverse Bits

Postby MrSwiss » Jul 30, 2020 22:59

ShawnLG wrote:Doticat, Nice way to do it but the "bits" are not put back into x. you gave me an idea on using strings.
No strings are needed for such a maniplulation.
Since the Functions are 'overload'ed, they can be adapted, to any numeric FB data-type.

Code: Select all


Function InvBits OverLoad(ByVal srcint As ULong) As ULong
    Dim As ULong reti = 0
   
    For i As UInteger = 0 To 31
        If Bit(srcint, 31 - i) Then reti = BitSet(reti, i)
    Next
   
    Return reti
End Function

Function InvBits OverLoad(ByVal srcint As ULongInt) As ULongInt
    Dim As ULongInt reti = 0
   
    For i As UInteger = 0 To 63
        If Bit(srcint, 63 - i) Then reti = BitSet(reti, i)
    Next
   
    Return reti
End Function

Function InvBits OverLoad(ByVal srcint As UInteger) As UInteger
    Dim As UInteger reti
   
    If SizeOf(UInteger) = 8 Then
        For i As UInteger = 0 To 63
            If Bit(srcint, 63 - i) Then reti = BitSet(reti, i)
        Next
    Else
        For i As UInteger = 0 To 31
            If Bit(srcint, 31 - i) Then reti = BitSet(reti, i)
        Next
       
    End If
   
    Return reti
End Function
   
   
Dim As ULongInt x = 100


Print Bin(x, 64)
print
Print "Now in inverted order."
Print
Print Bin(InvBits(x), 64)

Sleep
Last edited by MrSwiss on Jul 30, 2020 23:03, edited 1 time in total.
jj2007
Posts: 1523
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Reverse Bits

Postby jj2007 » Jul 30, 2020 23:01

ShawnLG wrote:I found the example here. https://aticleworld.com/5-way-to-reverse-bits-of-an-integer/
It is good practice to credit the source. Which is not the aticleworld site but rather, as mentioned in the Stanford page to which aticleworld links, Edwin Freed, 1983.

Return to “Tips and Tricks”

Who is online

Users browsing this forum: No registered users and 5 guests