adds bit manipulations over byte openarrays

This commit is contained in:
Zahary Karadjov 2018-11-26 19:16:00 +02:00 committed by zah
parent 481a3d3086
commit 543d53ac7e

View File

@ -114,6 +114,13 @@ template getAbsoluteBit(bytes, absIdx: untyped): bool =
getBitBE(bytes[byteToCheck], bitToCheck)
template setAbsoluteBit(bytes, absIdx, value) =
let
byteToWrite = absIdx shr 3 # the same as absIdx / 8
bitToWrite = (absIdx and 0b111)
setBitBE(bytes[byteToWrite], bitToWrite, value)
iterator enumerateBits(x: BitRange): (int, bool) =
var p = x.start
var i = 0
@ -123,6 +130,12 @@ iterator enumerateBits(x: BitRange): (int, bool) =
inc p
inc i
proc getBit*(bytes: openarray[byte], pos: Natural): bool =
getAbsoluteBit(bytes, pos)
proc setBitBE*(bytes: var openarray[byte], pos: Natural, value: bool) =
setAbsoluteBit(bytes, pos, value)
iterator items*(x: BitRange): bool =
for _, v in enumerateBits(x): yield v
@ -156,13 +169,8 @@ proc `==`*(a, b: BitRange): bool =
proc `[]=`*(r: var BitRange, idx: Natural, val: bool) {.inline.} =
assert idx < r.len
let
absIdx = r.start + idx
byteToWrite = absIdx shr 3 # the same as absIdx / 8
bitToWrite = (absIdx and 0b111)
setBitBE r.data[byteToWrite], bitToWrite, val
let absIdx = r.start + idx
setAbsoluteBit(r.data, absIdx, val)
proc setAbsoluteBit(x: BitRange, absIdx: int, val: bool) {.inline.} =
## Assumes the destination bit is already zeroed.