Skip to main content

Bitwise

Count no. of set bit

mask.bit_count()

All subsets of a mask

m = mask; s = []
while m:
s.append(m)
m = (m - 1) & mask

x if num = x * 2 ** y

x = num // (num & -num)

a ** b

res = 1
while b:
if b & 1: res = res * a
a *= a
b >>= 1