I have following's code:
import binarylang
import strformat
struct(i7, endian = l, bitEndian = r):
u1:
flag = 0
7:
value
block:
var sbs = newStringBitStream()
for v in -127 .. 127:
sbs.seek(0)
let data = I7(value: v.int8)
echo data
i7.put(sbs, data)
sbs.seek(0)
let data2 = i7.get(sbs)
echo data2
sbs.seek(0)
let s = sbs.readAll()
let c = s[0].uint8
echo fmt"{c:08b}"
# doAssert data.value == data2.value
The last line don't work. I have PR in github, but no response in 3 months. import streams import strformat import strutils #
You can always fork the repo, fix it in your fork while the maintainer respond.
If you use atlas for external dependencies it is quite easy to resolve the dependencies using your own fork instead of the original repo.
Had a look in binaryparse which is the precursor to binarylang. Seems like the bug is caused by the extraction of the lower field just using and and not extending the sign bit. You can manually extend the sign bit, but this should definitely be fixed upstream:
import binarylang
import strformat
struct(i7, endian = l, bitEndian = r):
u1:
flag = 0
7:
value
block:
var sbs = newStringBitStream()
for v in -64 .. 63:
sbs.seek(0)
let data = I7(value: v.int8)
echo data
i7.put(sbs, data)
sbs.seek(0)
var data2 = i7.get(sbs)
data2.value = ((data2.value shl 1) shr 1)
echo data2
sbs.seek(0)
let s = sbs.readAll()
let c = s[0].uint8
echo fmt"{c:08b}"
doAssert data.value == data2.value
P.S: Also fixed the range of values so we don't try to stuff an 8 bit value into a seven bit field.