简体   繁体   中英

Python Bits and bytes

I was wondering how i could extract the last 2 bits of a byte. 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' 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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