Skip to content

Instantly share code, notes, and snippets.

@zephenryus
Created October 16, 2018 19:49
Show Gist options
  • Save zephenryus/3aec76d84c09fe6d6ffc29a3daeafec5 to your computer and use it in GitHub Desktop.
Save zephenryus/3aec76d84c09fe6d6ffc29a3daeafec5 to your computer and use it in GitHub Desktop.
def get_mdb_position(value: int) -> tuple:
"""
Lookup for Moser-de Bruijn Sequence
https://en.wikipedia.org/wiki/Moser%E2%80%93de_Bruijn_sequence
:param value:
:return:
"""
if value <= 0:
return 0, 0
value = int(value)
x = 0
y = 0
mask = 1
offset = 0
maxInt = 2 ** (struct.Struct('i').size * 8 - 1) - 1
while True:
x |= value >> offset & mask
offset += 1
y |= value >> offset & mask
mask <<= 1
if value < 1 << offset + 1 or mask > maxInt:
break
return x, y
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment