The meaning of Bit-wise NOT in Python [duplicate]
问题 This question already has answers here : bit-wise operation unary ~ (invert) (5 answers) Closed last year . Why bitwise not does not act as expected for toggling bits? See for example below: a = 5 print(bin(a)) b = ~a print(bin(b)) This is the output: 0b101 -0b110 The question is why the first bit from the left is not toggled? Considering that Python documentation says: ~ x Returns the complement of x - the number you get by switching each 1 for a 0 and each 0 for a 1. Edit: Are you saying