title: Python representation of negative integers
body: <pre><code>&gt;&gt;&gt; x = -4
  &gt;&gt;&gt; print("{} {:b}".format(x, x))
  -4 -100
  &gt;&gt;&gt; mask = 0xFFFFFFFF
  &gt;&gt;&gt; print("{} {:b}".format(x &amp; mask, x &amp; mask))
  4294967292 11111111111111111111111111111100
  &gt;&gt;&gt; 
  &gt;&gt;&gt; x = 0b11111111111111111111111111111100
  &gt;&gt;&gt; print("{} {:b}".format(x, x))
  4294967292 11111111111111111111111111111100
  &gt;&gt;&gt; print("{} {:b}".format(~(x ^ mask), ~(x ^ mask)))
  -4 -100
  </code></pre>
  
  <p>I am having trouble figuring out how Python represents negative integers, and therefore how bit operations work. It is my understanding that Python attempts to emulate two's complement, but with any number of bits. Therefore, it is common to use 32-bit masks to force Python to set a standard size on integers before bit operations.</p>
  
  <p>As you can see in my example, <code>-4 &amp; 0xFFFFFFFF</code> yields a large positive number. Why does Python seem to read this as an unsigned integer, instead of a two's complement negative number? Later, the operation <code>~(x ^ mask)</code>, which should yield the exact same two's complement bit pattern as the large positive, instead gives <code>-4</code>. What causes the conversion to a signed int?</p>
  
  <p>Thanks!</p>