Python Forum
How to get the 2'complement of a number( type int8)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to get the 2'complement of a number( type int8)
#1
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 :)
Reply
#2
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?
Reply
#3
Thank you for replying. I have read the page you suggested but didn't solve my problem.
Luckily I found the solution later~~
Reply
#4
Quote:Luckily I found the solution later~~
Please share with the forum
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020