Skip to content

Instantly share code, notes, and snippets.

@blluv
Last active January 24, 2024 12:59
Show Gist options
  • Save blluv/7cb1b7cec39479bb453581137c11e9a1 to your computer and use it in GitHub Desktop.
Save blluv/7cb1b7cec39479bb453581137c11e9a1 to your computer and use it in GitHub Desktop.
class BitStream:
def __init__(self, data: bytes):
self.data = data
self.bitOffset = 0
def read_bits(self, bits: int):
if (bits + self.bitOffset) // 8 > len(self.data):
raise EOFError()
res = 0
for i in range(bits):
byte = self.data[self.bitOffset // 8]
bit = (byte >> self.bitOffset % 8) & 1
res |= bit << i
self.bitOffset += 1
return res
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment