With kotlin 1.5.21 installed on my path, in my she...
# announcements
e
With kotlin 1.5.21 installed on my path, in my shell when I run
kotlin
and then at the REPL prompt I type
(-1).toUInt()
or
1u - 2u
, then the result is
kotlin.UInt = -1
. Is that expected?
kotlin.UInt
ranges from 0 to 2^32-1, but can it be rendered as negative too somehow? Or is
kotlin.UInt = -1
actually equal to
2^32-1
?
Interestingly:
Copy code
>>> UInt.MAX_VALUE
res1: kotlin.UInt = -1
>>> UInt.MIN_VALUE
res2: kotlin.UInt = 0
Very paradoxical at first sight: the max value is less than the min value mind blown
It seems to be a rendering issue, where
Int.MAX_VALUE
is the maximum value being printed:
Copy code
>>> UInt.MAX_VALUE / 2u
res4: kotlin.UInt = 2147483647
>>> UInt.MAX_VALUE / 2u + 1u
res5: kotlin.UInt = -2147483648
e
seems to be printing the underlying Int representation
e
So it seems indeed, I found that
toString
just converts a
UInt
to a
Long
And the internal representation of a
UInt
is based on an
Int
, which can simply hold value
-1
, so that is what is printed
Oh, wait, that doesn't print -1, but correctly prints
Copy code
>>> (1u - 2u).toString()
res0: kotlin.String = 4294967295
So it is in the
println
somewhere...
Nope, also isn't
println
because that also prints the correct value
It's in the REPL that seems to not use
toString
Copy code
>>> UInt.MAX_VALUE.toString()
res1: kotlin.String = 4294967295
>>> UInt.MAX_VALUE
res2: kotlin.UInt = -1
>>> println(UInt.MAX_VALUE)
4294967295
a
hahahah, you gotta love these personal findings