Skip to content

Instantly share code, notes, and snippets.

@17twenty
Created October 1, 2014 18:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 17twenty/a1515072e20848a67f4e to your computer and use it in GitHub Desktop.
Save 17twenty/a1515072e20848a67f4e to your computer and use it in GitHub Desktop.
Integer complement calculation in Python
#!/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