Convert signed binary number into integer in Python
<ol>
<li><p>I found out that in python, if you use <code>bin</code>:</p>

<pre><code>&gt;&gt;&gt; bin(4)
'0b100'  
&gt;&gt;&gt; bin(-4)
'-0b100'
</code></pre></li>
<li><p>but if you try this:  </p>

<pre><code>-4 &gt;&gt; 8 =&gt; -1
</code></pre></li>
</ol>

<p>in fact, when right shift more than 3 bits, the answer is always <code>-1</code>. I know this is implementation stuff, but I came across this problem:</p>

<p>In the following array, find out the number which appears only once, while others appears always three times</p>

<pre><code>[-2,-2,1,1,-3,1,-3,-3,-4,-2]
</code></pre>

<p>This is the code</p>

<pre><code>class Solution(object):
    def singleNumber(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        if nums == None:
            return None
        ans = 0
        for i in range(32):
            numOfI = 0
            for num in nums:
                if (( num &gt;&gt; i) &amp; 1) == 1 :
                    numOfI += 1
                    numOfI %=3
            print i,numOfI
            if numOfI != 0:
                ans = ans + (numOfI &lt;&lt; i)
        return ans
</code></pre>

<p>the answer should be <code>-4</code>, but instead I get <code>11111111111111111111111111111100</code>; how can I convert it into -4?</p>