bitstruct icon indicating copy to clipboard operation
bitstruct copied to clipboard

No option for "signed or unsigned"

Open jbendes opened this issue 4 years ago • 3 comments

Would you be willing to accept a pullreq for adding functionality to allow either a signed or an unsigned number to be "packed"? There are cases where one would want to pack a number and its sign doesn't matter. For example, if you want to pack the bits 0b11 into 2 bits of a byte and you don't care if the number is treated as "3" or as "-1", there is currently no way to use this package to do so (that I can figure out at least)

jbendes avatar Nov 10 '21 14:11 jbendes

The closest I've been able to figure out is to use the unsigned pack format and bitmask the relevant number of bits. However, in addition to the added overhead of doing the bitmask where it's not needed, this strategy doesn't allow for syntax like this:

bitstruct.pack_into('>u1u1u1u1', *arr[:4]))

since you can't bitmask each element of the spread list

jbendes avatar Nov 10 '21 14:11 jbendes

I don't have any immediate problem with adding such functionality. Maybe pack('i2i2i1'), where i is for masked integer, which is always unpacked as unsigned?

eerimoq avatar Nov 11 '21 19:11 eerimoq

That sounds great!

jbendes avatar Nov 11 '21 20:11 jbendes