Skip to content

Instantly share code, notes, and snippets.

@matteobertozzi
Created December 26, 2011 19:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matteobertozzi/1521947 to your computer and use it in GitHub Desktop.
Save matteobertozzi/1521947 to your computer and use it in GitHub Desktop.
Zig-Zag/VInt Encoding
#!/usr/bin/env python
def encodeZigZag32(n): return (n << 1) ^ (n >> 31)
def encodeZigZag64(n): return (n << 1) ^ (n >> 63)
def decodeZigZag32(n): return (n >> 1) ^ -(n & 1)
def decodeZigZag64(n): return (n >> 1) ^ -(n & 1)
def encodeVInt(n):
data = ''
while n >= 0x80:
data += chr((n & 0x7f) | 0x80)
n >>= 7
data += chr(n)
return data
def decodeVInt(data):
shift = 0
result = 0
for c in data:
b = ord(c)
result |= ((b & 0x7f) << shift)
if not (b & 0x80):
break
shift += 7
return result
def lengthVInt(n):
length = 1
while n >= 0x80:
length += 1
n >>= 7
return length
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment