Hi, All
How to understand this disaster ?
Why second sub is wrong ?
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 ?
Last edited: