I summarized Kotlin basic number types and their range. In Kotlin, we can use Byte, Short, Int, Long, and UByte, UShort, UInt, ULong.
Size in Memory
Here, bit width and byte width for each number type.
| Type | ビット数 | バイト数 |
|---|---|---|
| Byte, UByte | 8 | 1 |
| Short, UShort | 16 | 2 |
| Int, UInt | 32 | 4 |
| Long, ULong | 64 | 8 |
| Float | 32 | 4 |
| Double | 64 | 8 |
Value Range
Here is the ranges of their values.
| Type | Min | Max |
|---|---|---|
Byte |
– 27 | 27 – 1 |
| – 128 | 127 | |
UByte |
0 | 28 – 1 |
| 255 | ||
Short |
– 215 | 215 – 1 |
| – 32768 | 32767 | |
UShort |
0 | 216 – 1 |
| 65535 | ||
Int |
– 231 | 231 – 1 |
| – 2147483648 | 2147483647 | |
UInt |
0 | 232 – 1 |
| 4294967295 | ||
Long |
– 263 | 263 – 1 |
| -9223372036854775808 | 9223372036854775807 | |
ULong |
0 | 264 – 1 |
| 18446744073709551615 | ||
Float |
– 3.4028235 x 1038 | 3.4028235 x 1038 |
Double |
– 1.7976931348623157 x 10308 | 1.7976931348623157 x 10308 |
Zeal number can take 2(bit width) numbers. Float, Double are calculated as below.
Float, Double Expression in Memory
Float, Double is separated into 3 part, sign part, exponent part, and fraction part in memory. The bit width of them are as follows.
| Sign | Exponent | Fraction | |
|---|---|---|---|
Float |
1 | 8 | 23 |
Double |
11 | 54 |
The value is expressed with the 3 values. Exponent value can be less than 0.
Now, denote the 3 parts as follows.
| (s) | Sign part value (0 or 1) |
|---|---|
| (e_b) | Bit width of exponent part (8 for Float, 11 for Double) |
| (e) | Exponent value in binary expression (from 0 to (2^{e_b} – 1)) |
| (f_b) | Bit width of fraction part (23 for Float, 54 for Double) |
| (f) | Fraction value in binary expression (from 0 to (2^{f_b} – 1)) |
Then, the value of Float or Double can be written as follows.
The value is the maximum when (s=0,e=2^{e_b}-1,f=2^{f_b}-1).
Here are some examples of sign, exponent and fraction parts in Float.
| 値 | Sign |
|---|---|
| Exponent | |
| Fraction | |
| 0.125 | 0 |
| 01111100 | |
| 00000000000000000000000 | |
| 0.25 | 0 |
| 01111101 | |
| 00000000000000000000000 | |
| 0.5 | 0 |
| 01111110 | |
| 00000000000000000000000 | |
| 1 | 0 |
| 01111111 | |
| 00000000000000000000000 | |
| 2 | 0 |
| 10000000 | |
| 00000000000000000000000 | |
| 3 | 0 |
| 10000000 | |
| 10000000000000000000000 | |
| 4 | 0 |
| 10000001 | |
| 00000000000000000000000 | |
| 5 | 0 |
| 10000001 | |
| 01000000000000000000000 | |
| 6 | 0 |
| 10000001 | |
| 10000000000000000000000 |
You can easily see binary expression of the value with toBits method, which I introduced in Kotlin 1.2 New Feature.





