数学类
尤其是一些矩阵运算与随机数的
随机
Fisher–Yates shuffle
位运算
separate rightest bit of 1, x & (-x)
unset rightest bit of 1, x & (x-1)
radix sort
int 转化为 bit字符串
bin(x)[2:]
或format(n, '08b')
Operation
Code
Example
Note
union
a | b
1010 | 0110 == 1110
difference
a & ~b
1010 & ~0110 == 1000
intersection
a & b
1010 & 0110 == 0010
add
a |= 1 << idx
1010 | 1 << 2 == 1110
idx
2 is 0-indexed from the right e.g. 3210
discard
a &= ~(1 << idx)
1010 & ~(1 << 3) == 0010
idx
3 is 0-indexed from the right e.g. 3210
contains?
bool(a & (1 << idx))
1010 & (1 << 3) == True
idx
3 is 0-indexed from the right e.g. 3210
Last updated