Python implements integers with arbitrary precision. When performing bit-wise operations, positive numbers are treated as if they have an infinite sequence of high-order 0
bits, while negative numbers act like they have an infinite sequence of high-order 1
bits. In particular, the number -1
acts like an infinite sequence of 1
's.
So for the first calculation we have
...11111111111111111111111111111111111111111111111111111111
& ...00000000000000000000000011111111111111111111111111111111
= ...00000000000000000000000011111111111111111111111111111111
Since this has 0
in the high-order bits, it's a positive result.
The second calculation is:
...11111111111111111111111111111111111111111111111111111111
^ ...00000000000000000000000100000000000000000000000000000000 (this is 0xffffffff + 1 = 0x100000000)
= ...11111111111111111111111011111111111111111111111111111111
Since this has 1
in the high-order bits, it's a negative result.