How do you decode a flag byte without returning its padding?

unpackbits expands unsigned bytes into individual bits. The count parameter limits the number of valid bits returned.

numpy.unpackbits in practice

Byte 160 begins with one, zero and one in high-bit-first order. Match bit order to the encoder's protocol before taking the first three flags.

import numpy as np
np.unpackbits(np.array([160],dtype=np.uint8),count=3,bitorder='big')

Result [1, 0, 1]

中文

末尾有填充位的标记字节怎样解码?

unpackbits 把 uint8 展开为逐位的零和一,count 可以限定解出的有效位数量。

numpy.unpackbits 算例

一个字节 160 以高位优先解释时,前三位为 1、0、1。如果编码协议采用低位优先,必须相应调整解码参数。

import numpy as np
np.unpackbits(np.array([160],dtype=np.uint8),count=3,bitorder='big')

结果 [1, 0, 1]

References / 参考来源

Keep reading / 继续阅读