按位操作符与背包问题的遍历
今天brute force 作业的案例和问题都十分有趣,而遍历的过程更能加深对按位操作符作用的理解。
Example
习题之前给的出的例子是单个背包组合遍历
# generate all combinations of N items
def powerSet(items):
N = len(items)
# enumerate the 2**N possible combinations
for i in range(2**N):
combo = []
for j in range(N):
# test bit jth of integer i
# ">>" is one bit right move in binary, equals divde 2**j