bit fields

This commit is contained in:
alexander.nutz
2024-03-25 11:25:05 +01:00
parent c4e90f4246
commit 59a288c849
4 changed files with 64 additions and 2 deletions

View File

@@ -0,0 +1,33 @@
package blitz
import blitz.collections.BitVec
import kotlin.reflect.KProperty
abstract class BitField {
private var vec = BitVec(1)
protected class BitDelegate(
private val thi: BitField,
private val pos: Int,
) {
operator fun getValue(thisRef: Any?, property: KProperty<*>): Boolean =
thi.vec[pos]
operator fun setValue(thisRef: Any?, property: KProperty<*>, value: Boolean) {
thi.vec[pos] = value
}
}
protected fun bit(pos: Int): BitDelegate =
BitDelegate(this, pos)
fun decode(byte: Byte) {
vec = BitVec.from(byteArrayOf(byte))
}
fun encode(): Byte =
vec.toBytes()[0]
override fun toString(): String =
"${this::class.simpleName}(0b${vec.toBytes()[0].toString(2)})"
}