blob: 6a410c1677c3f4b6364069785d5c4c07e4979382 [file] [log] [blame] [edit]
fun oneBit(index : Int) = 1 shl index
fun setBit(x : Int, index : Int) = x or oneBit(index)
fun unsetBit(x : Int, index : Int) = x and not(oneBit(index))
fun getBit(x : Int, index : Int) = x and oneBit(index) != 0
fun getBit(x : Int, index : Int) = (x shr index) shl 31 != 0
fun countOnes(x : INumber) {
var result = 0
while (x != 0) {
result += x and 1
x = x ushr 1
}
result
}
fun mostSignificantBit(x : INumber) = (x and oneBit(x.bits - 1) != 0) as Int
fun countOnes(x : INumber) = if (x == 0) 0 else mostSignificantBit(x) + countOnes(x shl 1)
fun Int.matchMask(mask : Int) = this and mask == mask
open class INumber<T : this> : IComparable<T> {
val bits : Int
[operator] fun plus(other : T) : T
[operator] fun shl(bits : Int) : T
// ...
}