Last active
November 26, 2019 00:41
-
-
Save ar45/38db0f7bb12ba50b1bd8bb17ae2533de to your computer and use it in GitHub Desktop.
Python 2s Compliment Binary Representation
This file contains hidden or 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 twosComp(val, bitlen=None): | |
if bitlen is None: | |
bitlen = int(val).bit_length() | |
val &= ( 2 << bitlen - 1 ) - 1 | |
return '{{:0{bitlen}b}}'.format(bitlen=bitlen).format(val) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment