Hi all,
at first: there's no hurry for my question!! I have a workaround. I only want to know, why this happens.
My question was also described by another user in 2013 too:
https://www.b4x.com/android/forum/threads/wrong-value-of-variable.31840/#post-185747
Another similar thread was there (Dec 2015):
https://www.b4x.com/android/forum/threads/variable-defined-as-long-acts-link-int.61817/#post-390078
But both threads couldn't answer my question (I didn't understand the answers).
I have the following sub:
Sub Test As Long
Dim i64Result As Long
Dim i32TempUpper As Int
Dim i32TempLower As Int
i32TempUpper = 2482035392
i32TempLower = 136775
'INCORRECT! -> Result = -7786483237554874368
i64Result = (i32TempUpper * 4294967296) + i32TempLower
'Correct Result = -7786483237554874809
i64Result = (i32TempUpper * 4294967296)
i64Result = i64Result + i32TempLower
Return i64Result
End Sub
In the old threads, the answer was, that I have to explicitly use long-variables in the formula to get the correct value. If I don't, then the compiler works with integers (32-bit), right?
So I think: because my variables are 32-Bit-integers, the compiler convert every result in 32-bit before doing the next math operation (the addition in my case).
Ok, I understand this.
But If I have a look on the binary bits of my results, the upper 32-bits are correct! Only the lower 32-bits are wrong. It seems, that the multiplication was correct and the addition fails. But if it is a convert-problem (long<->int), then the upper 32-bits (multiplication) should be wrong, not?
Bits of WRONG result (-7786483237554874368):
1001 0011 1111 0000 1101 1010 1100 0000
0000 0000 0000 0010 0001 1000 0000 0000
Bits of correct result (-7786483237554874809):
1001 0011 1111 0000 1101 1010 1100 0000
0000 0000 0000 0010 0001 0110 0100 0111
Again: I can work with my workaround. But I would be happy, if I could understand it
Thank you and have a great day!
Wolfgang