reactormonk
07/04/2022, 4:50 PMByte
to send them to the wire and back, and preferably a reasonable API along the way.Joffrey
07/04/2022, 4:53 PMEnumSet
implementation actually does use a bit mask internally IIRC.Klitos Kyriacou
07/04/2022, 5:05 PMreactormonk
07/04/2022, 5:08 PMByte
though 😞Joffrey
07/04/2022, 5:08 PMreactormonk
07/04/2022, 5:14 PMEnum
?Joffrey
07/04/2022, 8:32 PMordinal()
method generated for every enum class that gives the index of the current enum value within the declaration list.
But you probably wouldn't use that, and be more explicit instead by declaring a mask like so:
import java.util.EnumSet
import kotlin.experimental.*
enum class OpParameter(bitPosition: Int) {
ISO_TYPE_A(0),
ISO_TYPE_B(1),
FELICA_212(2),
FELICA_424(3),
TOPAZ(4),
CALYPSO(5),
SRIX(6),
RFU(7);
val mask: Byte = (1 shl bitPosition).toByte()
companion object {
private val values = values()
fun bitMaskToSet(bitMask: Byte) = values.filterTo(EnumSet.noneOf(OpParameter::class.java)) {
bitMask and it.mask > 0
}
}
}
fun Set<OpParameter>.toBitMask(): Byte = fold(0.toByte()) { result, op -> result or op.mask }
See https://pl.kotl.in/7tF9cTjj-reactormonk
07/04/2022, 9:00 PMinterface Positional {
abstract val position: Byte
fun toByte(): Byte = (1 shl position.toInt()).toByte()
}
enum class PICCOperating(override val position: Byte): Positional {
ISO14443TypeA(0),
ISO14443TypeB(1),
FeliCa212Kbps(2),
FeliCa424Kbps(3),
Topaz(4),
Calypso(5),
SRIX(6),
}
fun <A> EnumSet<A>.toBitMask(): Byte where A: Enum<A>, A: Positional {
return fold(0x00.toByte()) { b, e ->
b or e.toByte()
}
}
if
statement 🤔Joffrey
07/04/2022, 9:36 PMKlitos Kyriacou
07/04/2022, 9:48 PMreactormonk
07/04/2022, 10:06 PMephemient
07/05/2022, 6:28 AMreactormonk
07/06/2022, 9:25 AMfun bitMaskToSet(bitMask: Byte): EnumSet<AutomaticPPICPolling> =
values.filterTo(EnumSet.noneOf(AutomaticPPICPolling::class.java)) {
val toBitMask = it.toBitMask()
bitMask and toBitMask != 0.toByte()
}
The > 0
version behaves incorrectly with 0b10000000
- possibly because of a funny overflow?ephemient
07/06/2022, 5:57 PM0b10000000 == Byte.MIN_VALUE
is negative