0

My understanding was that ~ inverts all bits on the value specified. the binary representation of 10 is 00001010. If I invert all the bits I get 11110101 = 235. Why is -11 displayed? Where am I going wrong?

a = 10
print(a)
print(~a)   
# Output -11
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
PhyAH
  • 27
  • 3
  • https://en.wikipedia.org/wiki/Two's_complement. See https://wiki.python.org/moin/BitwiseOperators – John Coleman Feb 24 '21 at 12:04
  • 2
    In two's complement, bitwise negation of a positive number N gives you `-N-1`. See [What is “2's Complement”?](https://stackoverflow.com/q/1049722/3890632) – khelwood Feb 24 '21 at 12:04
  • What makes you think Python is using an unsigned 8-bit representation? – user2357112 Feb 24 '21 at 12:06
  • 1
    @user2357112supportsMonica Most students learn the basics of base 2 natural numbers long before they learn about two's complement (which is more of an implementation issue). It is the sort of thing many students would assume even without realizing that they are making an assumption. – John Coleman Feb 24 '21 at 12:09
  • Thank you all. I read the documents you suggested and I understand know. Your feedback is much appreciated! – PhyAH Feb 24 '21 at 13:54

0 Answers0