A simpler approach that I usually use is
d=bitarray('0'*30) d[5]=1 print(int(d.to01(),2))
The code is wise, perhaps not as efficient as it converts the bit array to a string and then back to int, but is much more concise to read, so it is probably better in shorter scenarios.
source share