Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Large numbers conversion to Byte in Kotlin

Tags:

kotlin

Why does a double of 65555 converted to byte produce a result of 19 in Kotlin?

like image 791
Alf Moh Avatar asked Dec 07 '25 08:12

Alf Moh


1 Answers

That's because of a numerical conversion from a wider type to a type of a smaller size. The Double (IEEE 754 double precision number) has its integral part factored to the powers of two as
65555 = 217 + 24 + 22 + 20 = 65536 + 16 + 2 + 1, which is stored in binary form as (higher bits to lower):

 ‭... 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 lowest 8 bits are retained:

 ... _ ‭_  _ _ _ _ _ _ _ _  0 0 0 1 0 0 1 1‬

And that results into 24 + 22 + 20 = 16 + 2 + 1 = 19.

like image 136
hotkey Avatar answered Dec 09 '25 13:12

hotkey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!