diff --git a/generators/builtins/unsignedTypes.kt b/generators/builtins/unsignedTypes.kt index 0653c62adba..8fb672183cd 100644 --- a/generators/builtins/unsignedTypes.kt +++ b/generators/builtins/unsignedTypes.kt @@ -199,7 +199,11 @@ class UnsignedTypeGenerator(val type: UnsignedType, out: PrintWriter) : BuiltIns for (otherType in UnsignedType.values()) { val name = otherType.capitalized out.print(" public fun to$name(): $name = ") - out.println(if (type == otherType) "this" else "data.to${otherType.capitalized}()") + out.println(when { + otherType > type -> "data.to${otherType.capitalized}() and ${type.mask}u" + otherType == type -> "this" + else -> "data.to${otherType.capitalized}()" + }) } out.println() } @@ -212,7 +216,6 @@ class UnsignedTypeGenerator(val type: UnsignedType, out: PrintWriter) : BuiltIns out.println("@ExperimentalUnsignedTypes") out.print("public fun $otherSigned.to$className(): $className = ") out.println(when { - otherType < type -> "$className(this.to$thisSigned() and ${otherType.mask})" otherType == type -> "$className(this)" else -> "$className(this.to$thisSigned())" }) diff --git a/libraries/stdlib/unsigned/src/kotlin/UByte.kt b/libraries/stdlib/unsigned/src/kotlin/UByte.kt index 4ed06b39565..a936e425aff 100644 --- a/libraries/stdlib/unsigned/src/kotlin/UByte.kt +++ b/libraries/stdlib/unsigned/src/kotlin/UByte.kt @@ -132,9 +132,9 @@ public inline class UByte internal constructor(private val data: Byte) : Compara public fun toLong(): Long = data.toLong() and 0xFF public fun toUByte(): UByte = this - public fun toUShort(): UShort = data.toUShort() - public fun toUInt(): UInt = data.toUInt() - public fun toULong(): ULong = data.toULong() + public fun toUShort(): UShort = data.toUShort() and 0xFFu + public fun toUInt(): UInt = data.toUInt() and 0xFFu + public fun toULong(): ULong = data.toULong() and 0xFFu public override fun toString(): String = toInt().toString() diff --git a/libraries/stdlib/unsigned/src/kotlin/UInt.kt b/libraries/stdlib/unsigned/src/kotlin/UInt.kt index bf6560144cc..5664db87ef2 100644 --- a/libraries/stdlib/unsigned/src/kotlin/UInt.kt +++ b/libraries/stdlib/unsigned/src/kotlin/UInt.kt @@ -138,7 +138,7 @@ public inline class UInt internal constructor(private val data: Int) : Comparabl public fun toUByte(): UByte = data.toUByte() public fun toUShort(): UShort = data.toUShort() public fun toUInt(): UInt = this - public fun toULong(): ULong = data.toULong() + public fun toULong(): ULong = data.toULong() and 0xFFFF_FFFFu public override fun toString(): String = toLong().toString() @@ -146,10 +146,10 @@ public inline class UInt internal constructor(private val data: Int) : Comparabl @SinceKotlin("1.3") @ExperimentalUnsignedTypes -public fun Byte.toUInt(): UInt = UInt(this.toInt() and 0xFF) +public fun Byte.toUInt(): UInt = UInt(this.toInt()) @SinceKotlin("1.3") @ExperimentalUnsignedTypes -public fun Short.toUInt(): UInt = UInt(this.toInt() and 0xFFFF) +public fun Short.toUInt(): UInt = UInt(this.toInt()) @SinceKotlin("1.3") @ExperimentalUnsignedTypes public fun Int.toUInt(): UInt = UInt(this) diff --git a/libraries/stdlib/unsigned/src/kotlin/ULong.kt b/libraries/stdlib/unsigned/src/kotlin/ULong.kt index 3cf24982049..f55d589e90b 100644 --- a/libraries/stdlib/unsigned/src/kotlin/ULong.kt +++ b/libraries/stdlib/unsigned/src/kotlin/ULong.kt @@ -146,13 +146,13 @@ public inline class ULong internal constructor(private val data: Long) : Compara @SinceKotlin("1.3") @ExperimentalUnsignedTypes -public fun Byte.toULong(): ULong = ULong(this.toLong() and 0xFF) +public fun Byte.toULong(): ULong = ULong(this.toLong()) @SinceKotlin("1.3") @ExperimentalUnsignedTypes -public fun Short.toULong(): ULong = ULong(this.toLong() and 0xFFFF) +public fun Short.toULong(): ULong = ULong(this.toLong()) @SinceKotlin("1.3") @ExperimentalUnsignedTypes -public fun Int.toULong(): ULong = ULong(this.toLong() and 0xFFFF_FFFF) +public fun Int.toULong(): ULong = ULong(this.toLong()) @SinceKotlin("1.3") @ExperimentalUnsignedTypes public fun Long.toULong(): ULong = ULong(this) diff --git a/libraries/stdlib/unsigned/src/kotlin/UShort.kt b/libraries/stdlib/unsigned/src/kotlin/UShort.kt index 57b64e0a202..e576a31d8f8 100644 --- a/libraries/stdlib/unsigned/src/kotlin/UShort.kt +++ b/libraries/stdlib/unsigned/src/kotlin/UShort.kt @@ -133,8 +133,8 @@ public inline class UShort internal constructor(private val data: Short) : Compa public fun toUByte(): UByte = data.toUByte() public fun toUShort(): UShort = this - public fun toUInt(): UInt = data.toUInt() - public fun toULong(): ULong = data.toULong() + public fun toUInt(): UInt = data.toUInt() and 0xFFFFu + public fun toULong(): ULong = data.toULong() and 0xFFFFu public override fun toString(): String = toInt().toString() @@ -142,7 +142,7 @@ public inline class UShort internal constructor(private val data: Short) : Compa @SinceKotlin("1.3") @ExperimentalUnsignedTypes -public fun Byte.toUShort(): UShort = UShort(this.toShort() and 0xFF) +public fun Byte.toUShort(): UShort = UShort(this.toShort()) @SinceKotlin("1.3") @ExperimentalUnsignedTypes public fun Short.toUShort(): UShort = UShort(this)