as we know
import bitOps
var x = 1'u8
x.setBits(0, 1, 2, 3)
echo x
says 15
and we can use HSlice to get the same result:
var x = 1'u8
for i in 0 .. 3:
x.setBits(i)
echo x
but we can't use x.setBits(0 .. 3) directly. So is there a way to unpack an HSlice to be use as varargs? In python, a list can be used like this
which says 1 2 3
thanks
Unfortunately, x.setBits(@[0, 1, 2, 3]) doesn't work for me. It is possible to use bitwise operations, though:
var x = 1'u8
let range = 0 .. 3
x = x or ((1'u8 shl len(range)) - 1'u8)
echo x # 15
Note that this only works if range looks like 0 .. n, and would require further modification to work with ranges like 4 .. 7.
proc setBits(x: var uint8, hs: HSlice) =
for i in hs:
x.setBits(i)