[공부용]참고 사이트 모음/[python]
[python]파이썬 2의 보수, bitstring 모듈 예시
bled
2020. 12. 10. 20:00
stackoverflow.com/questions/1604464/twos-complement-in-python
Two's Complement in Python
Is there a built in function in python which will convert a binary string, for example '111111111111', to the two's complement integer -1?
stackoverflow.com
It's not built in, but if you want unusual length numbers then you could use the bitstring module.
>>> from bitstring import Bits
>>> a = Bits(bin='111111111111')
>>> a.int
-1
The same object can equivalently be created in several ways, including
>>> b = Bits(int=-1, length=12)
It just behaves like a string of bits of arbitrary length, and uses properties to get different interpretations:
>>> print a.int, a.uint, a.bin, a.hex, a.oct
-1 4095 111111111111 fff 7777
Python, 2 의 보수 HEX 값으로 부터 int 변환하기, bitstring 모듈 사용
임베디드 프로그래밍을 하다 보면 레지스터 등의 설정을 2의 보수로 해야 하는 경우가 많이 있습니다. int 값을 2의 보수 헥사값으로 표현하거나 또는 2의 보수 헥사 값으로부터 int 값을 구하는
iamaman.tistory.com