22

I came across a situation just recently in which an unsigned integer would have been really useful (e.g. any negative value would not make sense etc.). Surprisingly, I discovered that Kotlin does not support unsigned integers - and there doesn't appear to be anything else out there about why (even though I've looked).

Am I missing something?

7
  • 1
    @nhaarman How is a Java answer related to a Kotlin question? May 7, 2018 at 19:59
  • 2
    @starbeamrainbowlabs did you just say what I think you did say?
    – Vivick
    May 7, 2018 at 20:25
  • 1
    @Vivick If you're referring to the fact that I don't fully understand how Java and Kotlin are related, then yes. May 7, 2018 at 20:26
  • 1
    Kotlin is transpiled down to Java, which is why there's no "real" support for unsigned integers. You can still hack your way with an Unsigned class but you'd encounter several issues.
    – Vivick
    May 7, 2018 at 20:29
  • 10
    @Vivick, Kotlin is NOT converted to Java source code. It's translated to JVM (Java Virtual Machine) bytecode. Don't disinformate people.
    – Serid
    Jan 31, 2019 at 21:45

3 Answers 3

34

Unsigned counterparts of Byte, Short, Int and Long do exist in Beta since Kotlin 1.3 and are stable as of Kotlin 1.5:

From the docs:

kotlin.UByte: an unsigned 8-bit integer, ranges from 0 to 255
kotlin.UShort: an unsigned 16-bit integer, ranges from 0 to 65535
kotlin.UInt: an unsigned 32-bit integer, ranges from 0 to 2^32 - 1
kotlin.ULong: an unsigned 64-bit integer, ranges from 0 to 2^64 - 1

Usage

// You can define unsigned types using literal suffixes
val uint = 42u 

// You can convert signed types to unsigned and vice versa via stdlib extensions:
val int = uint.toInt()
val uint = int.toUInt()
8

Why does Kotlin not have native unsigned types

This is because as this question shows, Java does not have built-in unsigned types.

When used on the JVM Kotlin compiles to Java Bytecode, so this limitation on Java also applies to Kotlin.

Workarounds

You can use the utility methods of Integer and Long to operate on values as unsigned link, but this still stores the values as signed internally.

You could also write a utility class that holds a value and acts like an unsigned int type, but this may be slower than using the method above.

1
  • 1
    I don't understand why the JVM dictates the functionality. If you can write the code to imitate an unsigned, then the byte code could do the same. Are you saying the code would be inefficient so the designers just didn't feel it was worth supporting it? It doesn't follow it can't be done. It might follow that it's not worth it.
    – Mitch
    Jan 29, 2019 at 9:46
8

Starting from Kotlin 1.3 unsigned types are available and based on inline classes feature.

See "Unsigned integer types" section of 1.3-M1 release: https://blog.jetbrains.com/kotlin/2018/07/see-whats-coming-in-kotlin-1-3-m1/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.