0

I have been trying to understand which representation is used by python for storing negative integers (1s complement, 2s complement or signed bit representation). For that, I tried out these functions

>>> bin(-10)
'-0b1010'
>>> bin(10)
'0b1010'
>>> format(10,'b')
'1010'
>>> format(-10,'b')
'-1010'
>>> format(-10,'16b')
'           -1010'
>>> format(-10,'32b')
'                           -1010'
>>> format(-10,'64b')
'                                                           -1010'

However, -1010 is not any of the above 3 representations (how is it storing the - symbol ?). The representation here I am getting is just - in front of the standard binary representation of the positive counterpart. So, how can I know the actual representation?

virmis_007
  • 173
  • 1
  • 4
  • 17
  • It's implementation-dependent - the "-" isn't being stored _anywhere_, it's just how Python displays values that are negative. – jonrsharpe Dec 28 '22 at 11:58
  • @jonrsharpe If "-" isn't stored anywhere then how does Python "remember" that the value is negative? – virmis_007 Dec 28 '22 at 11:59
  • Again, that's implementation-dependent. E.g. there are details for CPython in https://stackoverflow.com/q/23016610/3001761. – jonrsharpe Dec 28 '22 at 12:02
  • @jonrsharpe Okay, I checked you linked answer and understood. How can I close this question ? – virmis_007 Dec 29 '22 at 07:21

0 Answers0