Skip to content

Instantly share code, notes, and snippets.

@gboeer
Created December 3, 2021 14:54
Show Gist options
  • Save gboeer/945c9ccdc7efbf361844352fb14c89f4 to your computer and use it in GitHub Desktop.
Save gboeer/945c9ccdc7efbf361844352fb14c89f4 to your computer and use it in GitHub Desktop.
Convert an array or string of bits to decimal value
def bits_to_dec(arr):
"""
Works for all of those cases:
[False, True, True] --> 3
[1, 0, 1, 0] --> 6
"0111" --> 7
[1, '0', 0, False, '1', True] --> 35
"""
return int("".join([str(int(x)) for x in arr]), 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment