Last active
December 1, 2021 20:07
-
-
Save Bluefissure/3f6c20b21e254824caa227fb99cb676d to your computer and use it in GitHub Desktop.
Varint
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
def decodeVarint(x): | |
result = 0 | |
exit = False | |
while not exit: | |
if (x & 0b10000000): | |
exit = False | |
else: | |
exit = True | |
low_byte = x & (0b11111111) | |
result = result << 7 | |
result += x & (0b1111111) | |
x = x >> 8 | |
return result | |
def encodeVarint(x): | |
result = 0 | |
while x: | |
next_x = x >> 7 | |
temp_byte = 0b10000000 if next_x else 0 | |
temp_byte = temp_byte | (x & 0b1111111) | |
result = result << 8 | |
result = result | temp_byte | |
x = next_x | |
return result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment