- Learn Kotlin Programming(Second Edition)
- Stephen Samuel Stefan Bocutiu
- 208字
- 2021-06-24 14:13:25
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
}
推薦閱讀
- Building a Game with Unity and Blender
- Practical Internet of Things Security
- Visual FoxPro 程序設(shè)計(jì)
- Python高效開發(fā)實(shí)戰(zhàn):Django、Tornado、Flask、Twisted(第3版)
- Learning Network Forensics
- 零基礎(chǔ)Java學(xué)習(xí)筆記
- Rust游戲開發(fā)實(shí)戰(zhàn)
- Go語言底層原理剖析
- IoT Projects with Bluetooth Low Energy
- Software Development on the SAP HANA Platform
- 你好!Java
- Java EE輕量級(jí)解決方案:S2SH
- MATLAB從入門到精通
- Java基礎(chǔ)案例教程(第2版)
- Python數(shù)據(jù)分析與挖掘?qū)崙?zhàn)(第2版)