https://kotlinlang.org logo
#random
Title
# random
e

Edoardo Luppi

11/20/2023, 9:09 PM
Copy code
(-1L).toULong()
That's ugly sad panda Windows interop gone crazy AH, WAIT!
Copy code
(-1L).toULong() == ULong.MAX_VALUE
p

Pablichjenkov

11/20/2023, 9:26 PM
It seems that each platform uses a different integer binary bit representation. Is a good call out 👍
✔️ 2
k

Klitos Kyriacou

11/21/2023, 9:41 AM
What's strange about it? I'm not using Windows right now, but I always thought it was 64-bit two's complement, like most platforms.
p

Pablichjenkov

11/21/2023, 11:52 AM
I believe it uses twos complement. 111 = -1 which seeing from an unsigned perspective is the biggest possible value. Now is confusing, I consider this should throw or warn, since it is kinda converting something that is negative, and has no representation in the unsigned domain.
k

Klitos Kyriacou

11/21/2023, 12:08 PM
I think this is the correct behaviour as it is consistent with all other forms of integer casting in which the original number is outside the domain of the target. For example,
257.toByte()
== 1
p

Pablichjenkov

11/21/2023, 12:11 PM
Got it, makes sense tbh, is basically some of us not knowing the overflow/casting rules