karelpeeters
08/19/2019, 6:27 AMBooleanArray(64) { (long shr it) and 1 != 0 }
LastExceed
08/19/2019, 6:28 AMkarelpeeters
08/19/2019, 6:31 AMBitSet
from Java, it can convert back and forth to a long array.arr.foldIndexed(0L) { i, a, b -> a + if (b) 1 shr i else 0 }
LastExceed
08/19/2019, 6:37 AMkarelpeeters
08/19/2019, 6:38 AMfold
, it applies the lambda repeatedly to the previous result and the next element of the array. It's a common functional idiom, I'm sure there's lots of information about this online.LastExceed
08/19/2019, 6:39 AMkarelpeeters
08/19/2019, 6:39 AMsumByLong
which is missing from the stdlib I think.LastExceed
08/19/2019, 6:55 AMkarelpeeters
08/19/2019, 6:56 AMshl
.LastExceed
08/19/2019, 6:56 AMDico
08/19/2019, 10:30 AMLastExceed
08/19/2019, 10:31 AMLong
valuesRuckus
08/19/2019, 2:04 PMclass LongBit(private var bits: Long) {
operator fun get(index: Int) = bits shr index and 1L != 0L
operator fun set(index: Int, value: Boolean) {
val mask = 1L shl index
bits = if (value) bits or mask else bits and mask.inv()
}
}
LastExceed
08/19/2019, 2:05 PMRuckus
08/19/2019, 2:10 PMfun Long.getBit(index: Int) = shr(index) and 1L != 0L
fun Long.withBit(index: Int, bit: Boolean): Long {
val mask = 1L shl index
return if (bit) or(mask) else and(mask.inv())
}
fun Long.getBit(index: Int) = shr(index) and 1L != 0L
fun Long.withBit(index: Int, bit: Boolean): Long {
val mask = 1L shl index
return if (bit) or(mask) else and(mask.inv())
}
class LongBit(private var bits: Long) {
operator fun get(index: Int) = bits.getBit(index)
operator fun set(index: Int, bit: Boolean) {
bits = bits.withBit(index, bit)
}
override fun toString() = bits.toULong().toString(2).padStart(64, '0')
}