#coding:utf-8
mask = 0
for i in range(0,5):
# print ("1<<i "+str(i)+" :"+str(1<<i))
mask |= (1<<i)
print ("1<<i "+str(i)+" :"+str(1<<i)+"\t"+"mask "+str(i)+" :"+str(mask))
结果:
MacBook-Air:Downloads huangyong$ python3 test.py
1<<i 0 :1 mask 0 :1
1<<i 1 :2 mask 1 :3
1<<i 2 :4 mask 2 :7
1<<i 3 :8 mask 3 :15
1<<i 4 :16 mask 4 :31
MacBook-Air:Downloads huangyong$
原来是可以使用
|=
运算的