Skip to content

Instantly share code, notes, and snippets.

@Bluefissure
Last active December 1, 2021 20:07
Show Gist options
  • Save Bluefissure/3f6c20b21e254824caa227fb99cb676d to your computer and use it in GitHub Desktop.
Save Bluefissure/3f6c20b21e254824caa227fb99cb676d to your computer and use it in GitHub Desktop.
Varint
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