I wrote following code which performs well when input are all positive, but outputs a very big wrong answer if there is negative input:

test case:

a = [1,1,-3,1]

O: 4294967293

a = [1,2,3,2,2,1,1]

O: 3

I think this related to negative number binary representation in python. But I cannot figure out the details. Anybody can do me a favor? Many thanks!

```
def singleNumber(self,A):
n = len(A)
if n==0:
return A[0]
else:
result = 0
for j in range(32):
mask = 1 << j
sum = 0
for i in range(n):
if (A[i] >> j) & 1:
# bb = aa >> j
sum = sum + 1
output = sum%3
result = (output << j) | result
return result
```