Android Question Wrong value in Long-variables?

W. Graf

Member
Licensed User
Longtime User
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:
B4X:
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
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
I've tested this code in both debug and release modes and the result was the same (the correct value):
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Dim i64Result As Long
   Dim i32TempUpper As Int
   Dim i32TempLower As Int
   i32TempUpper = 2482035392
   i32TempLower = 136775
   i64Result = (i32TempUpper * 4294967296) + i32TempLower
   Log(i64Result)
   'Correct Result = -7786483237554874809
   i64Result = (i32TempUpper * 4294967296)
   i64Result  = i64Result + i32TempLower
   Log(i64Result)
End Sub
 
Upvote 0

W. Graf

Member
Licensed User
Longtime User
Hi Erel,

in Release-Mode, I couldn't reproduce it. In Debug-Mode, you can see it. But it is not so easy to reproduce this. I'm very confused about the phenomenas.
It seems, that there is a cache-problem too.
I used a real (physical) device.
Please try this:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Dim i64Result As Long
   Dim i32TempUpper As Int
   Dim i32TempLower As Int
   i32TempUpper = 2482035392
   i32TempLower = 136775
   i64Result = (i32TempUpper * 4294967296) + i32TempLower
   Log(i64Result)
   'Correct Result = -7786483237554874809
   i64Result = (i32TempUpper * 4294967296)
   i64Result  = i64Result + i32TempLower
   Log(i64Result)
End Sub
--> Start it, stop it and then start it again. Now you should get the same results (ending xxxx809).

So please change the code now:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Dim i64Result As Long
   Dim i32TempUpper As Int
   Dim i32TempLower As Int
   i32TempUpper = 2482035392
   i32TempLower = 136775
   i64Result = (i32TempUpper * 4294967296) + i32TempLower
   Log(i64Result)
   'Correct Result = -7786483237554874809
   i64Result = (i32TempUpper * 4294967296)
   i64Result  = i64Result + i32TempLower
   Log(i64Result)
  
   Dim x As String
   x = "BlaBla" 
End Sub
--> Now start again, you will get two different results (ending xxxxx368 and xxxxx809).

Note: sometimes it was vice versa: the first test returns two different values, and the second test two identical values -> I have no idea?!

Thank you!
Wolfgang
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…