Is there a better way to write these bit masks? ``...
# stdlib
g
Is there a better way to write these bit masks?
Copy code
val impulseDownAllowed: Boolean = capabilitiesByte and 0x10.toByte() != 0.toByte()
val bleMovementAllowed: Boolean = capabilitiesByte and 0x20.toByte() != 0.toByte()
e
I dealt a lot with that. The best I came up with is this:
Copy code
val impulseDownAllowed: Boolean = capabilitiesByte has 0x10
val bleMovementAllowed: Boolean = capabilitiesByte has 0x20
👍 2
another variant would be the
in
operator, but this requires operands inverted
g
is
has
your own infix extension function ?
e
yep
on the same logic, I also wrote
hasnt
g
I like the idea, thanks!
e
bitwise stuff is a pain in the ass in kotlin. This is one of the few thing Java is still better at.
consider voting https://youtrack.jetbrains.com/issue/KT-1440, hopefully we can get this sooner or later
g
I'd just be happy if I didn't have to do the
toByte()
call to be honest.
e
I also wrote a
.b
extension for that 😛
g
🤯
so now I want to make a function that will give me the value represented by some bits in a byte. so if I have a Byte 
0x10110110
 and the spec says 
Memory size: Bit 0-2
   I want to do something like 
byte.get(0..2) = 0b101
what would you call such a function? My best call right now is just
fun Byte.get(bitRange : IntRange) : Byte
e
hey Gerard, I missed you 😎
what about the get operator?
byte[0, 2]
btw, what are you doing in general? Encoding variable-length-bit data on bytes?
g
yeah, right now I'm "parsing" the bit data from some bytes
e
one note: since jvm converts integers (
Bytes
and
Shorts
) to
Int
(or
Long
when needed) to perform arithmetic operations, you should consider returning that
👍 1
e
Yup. You should not need to work with bytes. Bytes are only useful as a storage type when you have arrays with many of them. There’s literally zero need to use byte variables or functions taking/returing bytes for any kind of computation