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.
1 Answer 1
There is always the old fashioned trick of masking:
>>> bits = bin(byte[0] & 0x03)
>>> bits
'0b10'
answered Jan 25, 2015 at 18:03
Steve Barnes
28.6k6 gold badges68 silver badges80 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-py
0xFE. Please try to copy and paste actual transcripts where possible.