Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Endian swapping in QB64pe
#2
I tried to figure it out. I tried replacing all the _SHL, _SHR, AND, _READBIT and _SETBIT functions and in the end I got the same thing as with these functions. So I'll summarize. It's a perfect job for headaches. You can even see smoke coming from the ear. Your program even caused snow to melt in my area. I really don't know what the problem is!

Code: (Select All)


' 64 Bit endian swap, should print DEADBEEFCAFEF00D
' But instead prints...            FEFFFEEFCAFEF00D
Dim Shared As _Integer64 rax, rdx
rdx = &H0DF0FECAEFBEADDE
Print rdx
rax = mySHR64((rdx AND &HFF00000000000000), 56) OR _
      mySHR64((rdx AND &H00FF000000000000), 40) OR _
      mySHR64((rdx AND &H0000FF0000000000), 24) OR _
      mySHR64((rdx AND &H000000FF00000000),  8) OR _
      mySHL64((rdx AND &H00000000FF000000),  8) OR _
      mySHL64((rdx AND &H0000000000FF0000), 24) OR _
      mySHL64((rdx AND &H000000000000FF00), 40) OR _
      mySHL64((rdx AND &H00000000000000FF), 56)
Print Hex$(rax)

'32 Bit endian swap, should print DEADBEEF
'But instead prints...            FEADBEEF
Dim Shared As Long eax, edx
edx = &HEFBEADDE
eax = mySHR32((edx AND &HFF000000), 24) OR _
      mySHR32((edx AND &H00FF0000),  8) OR _
      mySHL32((edx AND &H0000FF00),  8) OR _
      mySHL32((edx AND &H000000FF), 24)
Print Hex$(eax)


' 32bit version: Left bit shift
Function MySHL32~& (x As _Unsigned Long, shift As Integer)
    Dim result As _Unsigned Long
    result = 0
    For i = 0 To 31
        ' If the bit at position i is set...
        If MyReadBit64(x, i) Then
            target = i + shift
            ' Set the target bit only if it fits in 32 bits
            If target <= 31 Then result = MySetBit64(result, target)
        End If
    Next i
    MySHL32 = result
End Function

' 32bit version: Right bit shift
Function MySHR32~& (x As _Unsigned Long, shift As Integer)
    Dim result As _Unsigned Long
    result = 0
    For i = 0 To 31
        If MyReadBit64(x, i) Then
            target = i - shift
            If target >= 0 Then result = MySetBit64(result, target)
        End If
    Next i
    MySHR32 = result
End Function

' 64bit version: Left bit shift
Function MySHL64~&& (x As _Unsigned _Integer64, shift As Integer)
    Dim result As _Unsigned _Integer64
    result = 0
    For i = 0 To 63
        If MyReadBit64(x, i) Then
            target = i + shift
            If target <= 63 Then result = MySetBit64(result, target)
        End If
    Next i
    MySHL64 = result
End Function

' 64bit version: Right bit shift
Function MySHR64~&& (x As _Unsigned _Integer64, shift As Integer)
    Dim result As _Unsigned _Integer64
    result = 0
    For i = 0 To 63
        If MyReadBit64(x, i) Then
            target = i - shift
            If target >= 0 Then result = MySetBit64(result, target)
        End If
    Next i
    MySHR64 = result
End Function
'maybe bug in ReadBit or Setbit? The same wrong result...

' Returns the value of the bit at position "pos" (0 = least significant bit) in a 64-bit number
Function MyReadBit64& (x As _Unsigned _Integer64, poss As Integer)
    Dim divisor As _Unsigned _Integer64
    divisor = 1
    For i = 1 To poss
        divisor = divisor * 2
    Next i
    MyReadBit64 = (x \ divisor) Mod 2
End Function

' Sets the bit at position "pos" in the 64-bit number x and returns the new number.
' If the bit is already set, returns the original number.
Function MySetBit64~&& (x As _Unsigned _Integer64, poss As Integer)
    Dim factor As _Unsigned _Integer64
    If MyReadBit64(x, poss) = 1 Then
        MySetBit64 = x
    Else
        factor = 1
        For i = 1 To poss
            factor = factor * 2
        Next i
        MySetBit64 = x + factor
    End If
End Function

again the same bad result....    Conclusion: I really don't know!


Reply


Messages In This Thread
Endian swapping in QB64pe - by tantalus - 03-22-2025, 10:19 AM
RE: Endian swapping in QB64pe - by Petr - 03-22-2025, 02:54 PM
RE: Endian swapping in QB64pe - by tantalus - 03-22-2025, 03:10 PM
RE: Endian swapping in QB64pe - by SMcNeill - 03-22-2025, 03:25 PM
RE: Endian swapping in QB64pe - by tantalus - 03-22-2025, 03:47 PM
RE: Endian swapping in QB64pe - by DSMan195276 - 03-22-2025, 04:31 PM
RE: Endian swapping in QB64pe - by SMcNeill - 03-22-2025, 03:42 PM
RE: Endian swapping in QB64pe - by SMcNeill - 03-22-2025, 04:01 PM
RE: Endian swapping in QB64pe - by Petr - 03-22-2025, 05:03 PM



Users browsing this thread: 1 Guest(s)