CLOVIS
06/14/2025, 10:21 AMphldavies
06/14/2025, 10:33 AMCLOVIS
06/14/2025, 10:35 AMphldavies
06/14/2025, 11:41 AM/**
* Converts this [Int] value to [UInt].
*
* If this value is positive, the resulting `UInt` value represents the same numerical value as this `Int`.
*
* The resulting `UInt` value has the same binary representation as this `Int` value.
*/
@SinceKotlin("1.5")
@WasExperimental(ExperimentalUnsignedTypes::class)
@kotlin.internal.InlineOnly
public inline fun Int.toUInt(): UInt = UInt(this)
It's documented as having the same binary representation (as indeed a uint32 and int32 have the same binary representation with the only difference being how the MSB is treated)CLOVIS
06/14/2025, 12:12 PMphldavies
06/14/2025, 12:19 PMCLOVIS
06/14/2025, 12:20 PMCLOVIS
06/14/2025, 12:22 PMphldavies
06/14/2025, 12:22 PMphldavies
06/14/2025, 12:24 PMCLOVIS
06/14/2025, 12:33 PMval counter: UInt
counter // 4294967295
.toULong() // 4294967295
.shl(22) // 18446744069414584320
.toLong() // -4294967296
Here, I don't want .toLong()
to change the value, I want to keep the same one (so I do want to change the binary representation, sorry)CLOVIS
06/14/2025, 12:35 PMCLOVIS
06/14/2025, 12:36 PMUInt
that I must combine into a single Long
(one UInt
is the 4 highest bytes, the other is the other four), and later extract both of them.phldavies
06/14/2025, 12:40 PMphldavies
06/14/2025, 12:45 PMCLOVIS
06/14/2025, 12:45 PMCLOVIS
06/14/2025, 12:46 PMval actual = counter
.also { println(it) }
.toULong()
.also { println(it) }
.shl(32)
.also { println(it) }
.toLong()
.also { println(it) }
.toULong()
.also { println(it) }
.shr(32)
.also { println(it) }
.toUInt()
.also { println(it) }
Klitos Kyriacou
06/16/2025, 9:25 AMshl
and ushr
on those.CLOVIS
06/16/2025, 9:42 AMUInt
for the counter and Long
for the full variable here, for reasons that are elsewhere in the program.loke
06/17/2025, 4:53 PM.toUInt()
(and related functions) does not change the values at all. It's just compile-time typing. If your two UInts are in the variables a
and b
, the solution to your problem is:
(a.toLong() shl 32) or b.toLong()
loke
06/17/2025, 4:53 PMCLOVIS
06/17/2025, 8:51 PMloke
06/19/2025, 1:57 PMloke
06/19/2025, 2:00 PMfun foo(a: Int) = a+1
and the other one being: fun bar(a: UInt) = a+1U
loke
06/19/2025, 2:00 PM