Skip to content

Instantly share code, notes, and snippets.

@dhbrojas
Created July 3, 2025 11:52
Show Gist options
  • Select an option

  • Save dhbrojas/ef578b098cdc73e07f47029eb9e62fb0 to your computer and use it in GitHub Desktop.

Select an option

Save dhbrojas/ef578b098cdc73e07f47029eb9e62fb0 to your computer and use it in GitHub Desktop.
import random
YEAR_SHIFT = 32
def encode(uid, year):
return (year << YEAR_SHIFT) | uid
def decode(docid):
return docid & ((1 << YEAR_SHIFT) - 1), docid >> YEAR_SHIFT
id = random.randint(0, 2**32)
year = random.randint(0, 2**16)
print(id, year)
enc = encode(id, year)
print(enc)
(id, year) = decode(enc)
print(id, year)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment