synic
synic

Reputation: 26668

How to split a Long into two Ints in Kotlin?

How can I split a Long (64bit) into two Integer (32bit) in Kotlin?

I've tried something like this but it doesn't seem to be doing it:

val id = Integer.MAX_VALUE.toLong() + 2000
val a = id.toInt()
val b = (id shr 32).toInt()

Upvotes: 1

Views: 484

Answers (1)

Ricky Mo
Ricky Mo

Reputation: 7648

Everything is working fine. Note that Integer.MAX_VALUE is 0x7FFFFFFF, when you add 2000, it becomes 0x800007CF, which is still within 32-bit, but overflow to the negative number range when interpreted as 32-bit signed integer. Therefore a is a negative Int and b is 0

Upvotes: 2

Related Questions