# Detailed Explanation of python Integer Overstepping Problem

- 2021-07-03 00:30:32
- OfStack

python has its own large integer operation ability, and integer operation will not overflow. As long as there is enough memory, oK

The following example demonstrates the addition of two 32-bit integers (by bit operation). In order to simulate the overflow effect, the bit operation must be performed manually. The ~ operator is also a binary complement operator besides negation. The binary digits after operation are interpreted according to the complement, for example ~ (0011 1100) = (1100 0011) =-61

```
def getSum(a, b):
"""
:type a: int
:type b: int
:rtype: int
"""
MAX = 0X7fffffff
MIN = 0X80000000
while b != 0 :
a,b = a^b,(a&b)<<1
print(" a = {0:b},b = {1:b}".format(a,b))
return a
def getSum_(a, b):
"""
:type a: int
:type b: int
:rtype: int
"""
MAX = 0x7FFFFFFF
MIN = 0x80000000
mask = 0xFFFFFFFF
while b != 0:
a, b = (a ^ b) & mask, ((a & b) << 1) & mask
print(type(a))
print(" a = {0:b},b = {1:b}".format(a,b))
return a if a <= MAX else ~(a^mask)
print(getSum_(-1,-1))
print(getSum(-1,1))
```

Supplement: Adding multiple conditions inside the python loop will judge that there will be out of bounds

1. Loop through the array. When you want to add conditional modifications, only delete the first one

```
# -*- coding: utf-8 -*-
a=[11,22,33,44,55]
for i in a:
if i == 11 or i ==22:
a.remove(i)
for i in a:
print(i)
'''
33
55
[Finished in 0.1s]
'''
```

2. You should introduce an array that was deleted to 1

```
# -*- coding: utf-8 -*-
a=[11,22,33,44,55]
b=[]
for i in a:
if i == 11 or i ==22:
b.append(i)
for i in b:
a.remove(i)
for i in a:
print(i)
'''
33
44
55
[Finished in 0.1s]
'''
```