B4J Question B4J and 64 bits

giggetto71

Active Member
Licensed User
Longtime User
Hi need some guidance here.
I am developing a Zobrist hashing algorithm (https://en.wikipedia.org/wiki/Zobrist_hashing).
for the implementation, one needs to generate a series of random numbers and then XOR them. one can use 32 bits to generate the random numbers but it would be much better to be able to use use 64 bit.
a conceptual simplified idea (the actual alg needs 64 random numbers and then they are XORed is:
B4X:
Dim MaxInt As Int,rndint As Int,key As Int, ii As Int
    
    MaxInt = Power(2,31)
     
    For ii = 0 To 63
       rndint = Rnd(0,MaxInt)
       key=Bit.Xor(key,rndint)               
    Next
    
.....

Is there a possibility to turn everything to 64bits in B4J? I have tried the below code but it crashes complaining that the rnd function bound must be positive. I guess that has to do with the fact that Java does not support unsigned type..


B4X:
Dim MaxLong As Long,rndlong As Int,keylong As Int, ii As Int
    
    MaxLong = Power(2,63)
    
    For ii = 0 To 63
       rndlong = Rnd(0,MaxLong)
       key=Bit.Xor(key,rndlong)               
    Next

do you see potential workarounds?
thanks!
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
There is no relation between the OS architecture and the types. A Long number is always a 64 bit number and an Int number is always 32 bit.

It also has nothing to do with unsigned types.

Bit.Xor works with Bytes not Longs.
Rnd works with Ints.

You can use ArrayXor: https://www.b4x.com/android/forum/t...xor-between-array-of-bytes.129989/post-817766

B4X:
Dim l1(8), l2(8) As Byte
    Dim sr As SecureRandom 'encryption library
    sr.GetRandomBytes(l1)
    sr.GetRandomBytes(l2)
    Dim l3() As Byte = ArrayXor(l1, l2)
You can use ByteConverter to convert the array of bytes to numbers.
 
Upvote 0
Top