python 比特输出_Python:从一个字节中提取比特

我正在阅读Python中的二进制文件,文件格式的文档说:

Flag (in binary)Meaning

1 nnn nnnn Indicates that there is one data byte to follow

that is to be duplicated nnn nnnn (127 maximum)

times.

0 nnn nnnn Indicates that there are nnn nnnn bytes of image

data to follow (127 bytes maximum) and that

there are no duplications.

n 000 0000 End of line field. Indicates the end of a line

record. The value of n may be either zero or one.

Note that the end of line field is required and

that it is reflected in the length of line record

field mentioned above.

在阅读文件时,我期待我返回1 nnn nnnn的字节,nnn nnnn部分应该是50。

我已经能够做到这一点使用以下内容:

flag = byte >> 7

numbytes = int(bin(byte)[3:], 2)但是,numbytes计算感觉像是一个便宜的解决方法。

我可以做更多的数学计算来完成numbytes的计算吗?

你会如何处理这个问题?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值