如何打包&解包64位数据?

时间:2016-02-06 18:17:58

标签: python bitvector

我有一个64位数据结构如下:

  

HHHHHHHHHHHHHHHHGGGGGGGGGGGGFFFEEEEDDDDCCCCCCCCCCCCBAAAAAAAAAAAA

A:12位(无符号)
B:1位
C:12位(无符号)
D:4位(无符号)
E:4位(无符号)
F:3位(无符号)
G:12位(无符号)
H:16位(无符号)

使用Python,我试图确定我应该使用哪个模块(最好是原生Python 3.x)。我正在考虑BitVector,但却无法搞清楚一些事情。

为了便于使用,我希望能够执行以下操作:

# To set the `A` bits, use a mapped mask 'objectId'
bv = BitVector(size=64)
bv['objectId'] = 1

我不确定BitVector是否按照我想要的方式运行。无论我最终实现什么模块,数据结构都将封装在一个类中,该类通过属性getter / setter读取和写入结构。

我还将使用常量(或枚举)来表示某些位值,并且可以使用以下内容设置映射掩码:

# To set the 'B' bit, use the constant flag to set the mapped mask 'visibility'
bv['visibility'] = PUBLIC  
print(bv['visibility']) # output: 1  

# To set the 'G' bits, us a mapped mask 'imageId'
bv['imageId'] = 238  

3.x中是否有Python模块可以帮助我实现这一目标?如果BitVector将(或应该)工作,一些有用的提示(例如示例)将不胜感激。似乎BitVector想要将所有内容强制为8位格式,这对我的应用程序来说并不理想(恕我直言)。

1 个答案:

答案 0 :(得分:0)

根据使用bitarray的建议,我使用两种实用方法提出了以下实现:

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
    for 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)

输出如下:

  

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

如果实用方法有所改进,我非常愿意接受一些建议。

相关问题