简体   繁体   English

Python位和字节

[英]Python Bits and bytes

I was wondering how i could extract the last 2 bits of a byte. 我想知道如何提取字节的最后2位。 I receive the bytes when reading in from a file. 从文件读取时收到字节。

byte = b'\\xfe' bits = bin(byte)

output: 0b00110001 

I want to know how i can 7th and 8th bit from that. 我想知道我如何才能做到这一点。

Any help would be appreciated. 任何帮助,将不胜感激。

There is always the old fashioned trick of masking: 总是有老式的掩盖技巧:

>>> bits = bin(byte[0] & 0x03)
>>> bits
'0b10' 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM