Created
December 3, 2021 14:54
-
-
Save gboeer/945c9ccdc7efbf361844352fb14c89f4 to your computer and use it in GitHub Desktop.
Convert an array or string of bits to decimal value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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