小编jmm*_*jmm的帖子

python3中的高效比特点击

我在python3中处理bitswapping非常困难.

到目前为止,我在这里这里找到了C中的快速bitwapping算法,但我无法正确地将其转换为python3,因为处理数据,使用正确的数据类型而不会被不同的编码混淆对我来说是不可能的.

对我有用的唯一解决方案是使用BitArray进行这样的交换:

with open(file_swapped, 'wb') as out, open(file, 'rb') as inp:
    byte_in = inp.read(1)
    while byte_in:
        tmp = bitstring.BitArray(byte_in)
        tmp.reverse()
        byte_out = tmp._getbytes()
        byte_in = inp.read(1)
Run Code Online (Sandbox Code Playgroud)

但是,此算法需要2分钟以上的时间来处理需要进行比特换行的数据.该算法的分析表明,BitArray的创建占用了总时间的大部分时间.

每次尝试将二进制输入数据转换为'0'和'1'或整数的字符串,手动执行"交换"-part失败,因为数据没有特定的编码(utf-8/utf-16 didn'工作)

这是我输入数据的一个例子

有没有人知道一个快速的方法,做上述任务?

python algorithm bit-manipulation python-3.x

4
推荐指数
1
解决办法
179
查看次数

标签 统计

algorithm ×1

bit-manipulation ×1

python ×1

python-3.x ×1