Created
July 6, 2017 15:51
-
-
Save giuscri/6cec5b940a854c3dedc07c8a14eded15 to your computer and use it in GitHub Desktop.
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 zero_left_pad(s, l=32): | |
""" | |
Pad a string with '0' such that | |
the resulting length equals to | |
a fixed amount. | |
Args: | |
s (str): string to pad | |
l (int): length of the resulting string | |
Returns: | |
str: string padded on the left with '0' | |
""" | |
while len(s) < l: s = '0' + s | |
return s | |
def unot(n): | |
""" | |
Compute the unsigned binary not of a number. | |
Args: | |
n (int) | |
Returns: | |
int: computed unsigned binary not | |
""" | |
binary_pattern = zero_left_pad(bin(n)[2:]) | |
inverted_pattern = ''.join( | |
map(lambda d: '0' if d == '1' else '1', binary_pattern) | |
) | |
return int(inverted_pattern, 2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment