官术网_书友最值得收藏!

Unsigned integers

Unsigned integer types were introduced in Kotlin 1.3. JVM does not have support out of the box for such types. Starting with Java SE 8, you can use the integer class to store an unsigned int, and operations for unsigned values are provided. Introducing these types, which are found in languages such as C++ or C#, is still in experimental mode. Here is the full list of these types:

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


These types can be defined using literal suffixes and, of course, converting between signed and unsigned types is possible. Last but not least, the unsigned types support similar operators, just like their signed counterparts:

@ExperimentalUnsignedTypes
fun sample() {
// You can define unsigned types using literal suffixes
val uint = 1094u
val ulong = 9999uL
val ubyte: UByte = 255u

val int = uint.toInt()
val byte = ubyte.toByte()
val ulong2 = byte.toULong()

val x = 20u + 22u
val y = "128".toUByte()
val range = 1u..5u
}
主站蜘蛛池模板: 丹阳市| 汕尾市| 花莲县| 彭泽县| 山东省| 河南省| 神农架林区| 乳山市| 齐齐哈尔市| 英德市| 寿宁县| 万全县| 甘泉县| 海丰县| 岗巴县| 神农架林区| 绥滨县| 合川市| 额尔古纳市| 特克斯县| 沈丘县| 伊宁县| 永济市| 蒲城县| 启东市| 师宗县| 密山市| 金门县| 林周县| 安国市| 阜阳市| 衡山县| 中西区| 哈密市| 嘉善县| 义马市| 瓮安县| 安康市| 洛阳市| 沁阳市| 平江县|