Created
October 1, 2014 18:08
-
-
Save 17twenty/a1515072e20848a67f4e to your computer and use it in GitHub Desktop.
Integer complement calculation in Python
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
#!/usr/bin/env python | |
# This would probably be the easiest way to do what you need | |
def getIntegerComplement (n): | |
# Setup mask of bits, can't use tilde as python uses signed :-/ | |
mask = (1 << n.bit_length()) - 1 | |
return n ^ mask | |
print getIntegerComplement(50) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment