python - How to pack & unpack 64 bits of data? -


i have 64-bit data structure follows:

hhhhhhhhhhhhhhhhggggggggggggfffeeeeddddccccccccccccbaaaaaaaaaaaa

a: 12 bits (unsigned)
b: 1 bit
c: 12 bits (unsigned)
d: 4 bits (unsigned)
e: 4 bits (unsigned)
f: 3 bits (unsigned)
g: 12 bits (unsigned)
h: 16 bits (unsigned)

using python, trying determine module (preferably native python 3.x) should using. looking @ bitvector having trouble figuring things out.

for ease of use, want able following:

# set `a` bits, use mapped mask 'objectid' bv = bitvector(size=64) bv['objectid'] = 1 

i'm not sure bitvector works way want to. whatever module end implementing, data structure encapsulated in class reads , writes structure via property getters/setters.

i using constants (or enums) of bit values , convenient able set mapped mask using like:

# set 'b' bit, use constant flag set mapped mask 'visibility' bv['visibility'] = public   print(bv['visibility']) # output: 1    # set 'g' bits, mapped mask 'imageid' bv['imageid'] = 238   

is there python module in 3.x me achieve goal? if bitvector (or should) work, helpful hints (e.g. examples) appreciated. seems bitvector wants force 8-bit format not ideal application (imho).

based on recommendation use bitarray have come following implementation 2 utility methods:

def test_bitvector_set_block_id_slice(self):     bv = bitvector(vector_size)     bv.setall(false)      print("bitvector[{len}]: {bv}".format(len=bv.length(),                                           bv=bv.to01()))     print("set block id: current {bid} --> {nbid}".format(bid=bv[52:vector_size].to01(),                                                           nbid=inttobitvector(12, 1).to01()))      # set blockvector.blockid (last 12 bits)     bv[52:vector_size] = inttobitvector(12, 1)      block_id = bv[52:vector_size]      self.asserttrue(bitvectortoint(block_id) == 1)     print("bitvector[{len}] set block id: {bin} [{val}]".format(len=bv.length(),                                                                 bin=block_id.to01(),                                                                 val=bitvectortoint(block_id)))     print("bitvector[{len}]: {bv}".format(len=bv.length(),                                           bv=bv.to01()))     print()  # utility methods def bitvectortoint(bitvec):     out = 0     bit in bitvec:         out = (out << 1) | bit      return out   def inttobitvector(size, n):     bits = "{bits}".format(bits="{0:0{size}b}".format(n,                                                       size=size))      print("int[{n}] --> binary: {bits}".format(n=n,                                                bits=bits))      return bitvector(bits) 

the output follows:

bitvector[64]: 0000000000000000000000000000000000000000000000000000000000000000
int[1] --> binary: 000000000001
set block id: current 000000000000 --> 000000000001
int[1] --> binary: 000000000001
bitvector[64] set block id: 000000000001 [1]
bitvector[64]: 0000000000000000000000000000000000000000000000000000000000000001

if there improvements utility methods, more willing take advice.


Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -