B4R Question Why Dim mask As ULong = 2147483648 <> 0x80000000 ?

peacemaker

Expert
Licensed User
Longtime User
Hi, All

How to understand this disaster ?


B4X:
Private Sub AppStart
    Serial1.Initialize(115200)
    Delay(2000)
    Log(CRLF, "AppStart")
 
    Log(BinaryToString_ULong(1))
    Log(BinaryToString_ULong2(1))
End sub

Sub BinaryToString_ULong(value As ULong) As String
    Dim result As String = "0b"
    Dim mask As ULong = 2147483648 'highest bit = 1, 2147483648
    Dim i As Byte

    For i = 0 To 31
        If BitAnd_ULong(value, mask) <> 0 Then
            result = JoinStrings(Array As String(result, "1"))
        Else
            result = JoinStrings(Array As String(result, "0"))
        End If

        'space between
        If (i Mod 8) = 7 And i < 31 Then
            result = JoinStrings(Array As String(result, " "))
        End If

        'shift left as 32-bit
        mask = mask / 2
    Next

    Return result
End Sub

Sub BinaryToString_ULong2(value As ULong) As String
    Dim result As String = "0b"
    Dim mask As ULong = 0x80000000 'highest bit = 1, 2147483648
    Dim i As Byte

    For i = 0 To 31
        If BitAnd_ULong(value, mask) <> 0 Then
            result = JoinStrings(Array As String(result, "1"))
        Else
            result = JoinStrings(Array As String(result, "0"))
        End If

        'space between
        If (i Mod 8) = 7 And i < 31 Then
            result = JoinStrings(Array As String(result, " "))
        End If

        'shift left as 32-bit
        mask = mask / 2
    Next

    Return result
End Sub

Sub BitAnd_ULong(a As ULong, b As ULong) As ULong
    Dim aLow As UInt = a Mod 65536
    Dim aHigh As UInt = a / 65536
    Dim bLow As UInt = b Mod 65536
    Dim bHigh As UInt = b / 65536

    Dim resLow As UInt = Bit.And(aLow, bLow)
    Dim resHigh As UInt = Bit.And(aHigh, bHigh)

    Return (resHigh * 65536) + resLow
End Sub

New upload port: COM15 (serial)
AppStart
0b00000000 00000000 00000000 00000001
0b00000000 00000000 00000000 00000000

Why second sub is wrong ?
1752146765675.png
1752146806575.png
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
About these outputs: when you don't explicitly set the numeric types, the compiler chooses them automatically. In this case it treats the numbers as ints or floats (https://www.b4x.com/android/forum/threads/data-types.65666/#content) and you get these results. The compiler can be improved here.

Explicitly setting the type will make it return the expected values:
B4X:
Dim ul As ULong = 4294967295
Log(ul)
ul = 0xFFFFFFFF
Log(ul)
Dim ui As UInt = 65535
Log(ui)
Log((0xFFFF).as(UInt))
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
But something wrong is in the IDE with parsing HEX: different result if to use

B4X:
LogNumber("HighWord1", BitHighWord(305419896))    'works OK
LogNumber("HighWord2", BitHighWord(0x12345678))    'works wrong

1752171147477.png


@Erel , could you check the project ?
 

Attachments

  • b4r_bitShift32_v0.53.zip
    2.6 KB · Views: 35
Last edited:
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Wrongly works even if declare the var by HEX constant!
B4X:
Dim source3 As ULong = 0x12345678
LogNumber("HighWord2", BitHighWord(source3))    'works also wrong !
 
Upvote 0
Top