Skip to content

Instantly share code, notes, and snippets.

@darius
Last active July 5, 2020 14:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darius/4733be9d85494708c7182708e763bf3f to your computer and use it in GitHub Desktop.
Save darius/4733be9d85494708c7182708e763bf3f to your computer and use it in GitHub Desktop.
"""
Problem from David Albert
'bumps' idea from Michael Arntzenius & Eugene Ha
"""
def repr_register_bits(bits):
return '/'.join(repr_bits('D', bits & 0xFF) + repr_bits('A', bits >> 8))
def repr_bits(label, bits):
bits = [(bits>>i) & 1 for i in range(8)]
bumps = [next_b - b for b,next_b in zip([0]+bits, bits+[0])]
return [repr_range(label, r) for r in zip(where(1, bumps), where(-1, bumps))]
def repr_range(label, (start, after)):
i, j = start, after-1
return '%s%d' % (label, i) if i==j else '%s%d-%s%d' % (label, i, label, j)
def where(target, things):
return [i for i,thing in enumerate(things) if thing == target]
## repr_register_bits(0x1221)
#. 'D0/D5/A1/A4'
## repr_register_bits(0xfc2f)
#. 'D0-D3/D5/A2-A7'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment