Converting large numbers to bytes in Kotlin
3 answers
This is due to the numerical conversion from a wider type to a smaller type. Double
( IEEE 754 double precision number ) has its integral part, counted in powers of two, like 65555 = 2 17 + 2 4 + 2 2 + 2 0 = 65536 + 16 + 2 + 1, which is stored in binary form (upper digits below):
... 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 1
When this number is converted to Byte
, only its lower 8 bits are kept:
... _ _ _ _ _ _ _ _ _ _ 0 0 0 1 0 0 1 1
And that leads to 2 4 + 2 2 + 2 0 = 16 + 2 + 1 = 19.
+9
source to share