0

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.

jamesb1082
  • 35
  • 2
  • possible duplicate of [Convert bytes to bits in python](http://stackoverflow.com/questions/8815592/convert-bytes-to-bits-in-python) – blackbird Jan 25 '15 at 16:32
  • 4
    I don't think that code would work in either Python 2 or Python 3, and even if it did, that's not the binary associated with `0xFE`. Please try to copy and paste actual transcripts where possible. – DSM Jan 25 '15 at 16:36

1 Answers1

2

There is always the old fashioned trick of masking:

>>> bits = bin(byte[0] & 0x03)
>>> bits
'0b10' 
Steve Barnes
  • 27,618
  • 6
  • 63
  • 73