Skip to content

Instantly share code, notes, and snippets.

@giuscri
Created July 6, 2017 15:51
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 giuscri/6cec5b940a854c3dedc07c8a14eded15 to your computer and use it in GitHub Desktop.
Save giuscri/6cec5b940a854c3dedc07c8a14eded15 to your computer and use it in GitHub Desktop.
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