Python Forum
How to get the 2'complement of a number( type int8) - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: How to get the 2'complement of a number( type int8) (/thread-8733.html)



How to get the 2'complement of a number( type int8) - zengshh - Mar-05-2018

Hello everyone,
This is my first thread in python forum, thank you for reading.
I want to get the 2'complement of an integer, which is the actual representation in the computer. I have tried many methods but failed.Here are some method I tried:
num_a = numpy.int8(-12)
print(format(num_a, 'b'))
print(bin(num_a))

the two outputs are " - 0b10100" and " - 10100 ", instead of "10010011" as I expect. But it is right if the digit is between 0 and 127. Technically , they didn't produce the 2'complement of integer.
By now, I am confused that if the integer is stored in computer using its 2'complement. Is there functions suitable for my question?
I am waiting for your suggestion all the time. Thanks a lot :)


RE: How to get the 2'complement of a number( type int8) - Larz60+ - Mar-05-2018

In assembly language, you have to be concerned with two's complement, but not really otherwise.
I haven't even thought about 2's compliment since the late 70's.
But, since you're interested have you read: https://wiki.python.org/moin/BitwiseOperators?


RE: How to get the 2'complement of a number( type int8) - zengshh - Mar-10-2018

Thank you for replying. I have read the page you suggested but didn't solve my problem.
Luckily I found the solution later~~


RE: How to get the 2'complement of a number( type int8) - Larz60+ - Mar-10-2018

Quote:Luckily I found the solution later~~
Please share with the forum