Last active
January 24, 2024 12:59
-
-
Save blluv/7cb1b7cec39479bb453581137c11e9a1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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