0
>>>a='11111111111111111111111111110101'
>>>int(a,2)
4294967285

In the above code, I thought that 32'nd number is MSB(Most Significant Bit) and if MSB is 1,
The value indicates negative.
but why does the int() function return positive number?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578

1 Answers1

2

In Python that works a bit different, it is using two complements. I think the answer to the question here is what you are looking for and answers the 'why?'. You can test that yourself by using the bin()-function which returns you the bit equivalent of a number.

a='11111111111111111111111111110101'
print(int(a,2))
#4294967285

# Postive Integer 
# Same as above
b='0b11111111111111111111111111110101'
print(int(b,2))
#4294967285

# Negative Integer
=c'-0b11111111111111111111111111110101'
print(int(c,2))
#-4294967285

David
  • 116
  • 2
  • 10