Why Kotlin does not have built in support for bina...
# announcements
o
Why Kotlin does not have built in support for binary operations on Byte ? I tried code like this
Copy code
val b1: Byte = 2
        val b3: Byte = 7
        val b4 = b1 xor b3
and IDEA suggested
import kotlin.experimental.xor
, but when I tried this:
Copy code
val b1: Byte = 2
        val b3: Byte = 7
        val b4 = b1 shl b3
it does not have even anything to suggest. It is sad as I’m implementing CRC calculation that requires quite a lot of binary operations on bytes an each time I have to cast my bytes…. I do realise that it is possible to write extension. But I’m still wandering why Byte is not treated the same way Int and Long is?
d
I had the same question when I parsed our company' s internal binary network protocol.
I was fortunate and there were not so much Bytes, so I wrote an extension function.
k
It's on the todo list as part of a bigger thing with unsigned types etc.
At least that was the explanation half a year ago.
o
Grate to here it @karelpeeters BTW I’m converting CRC Calculation in to multiplatform project and can not find an easy way to convert string to ByteArray. On JVM it’s as easy as
"123".toByteArray()
^
PlatformUtils
in the serialization lib has
String.toUtf8Bytes()
if you don't want the whole lib as a dependency should be easy to copy-paste