비트연산
arr = [1,2,3]
n = len(arr)
for i in range(1<<n):
print(i,end = ' ')
for j in range(n):
if i & (1<<j):
print(arr[j], end = ',')
print()
print()
(1) (1<<n)
(2) for i in range(1<<n)
- 0 : 000
- 1 : 100
- 2 : 010
- 3 : 110
- 4 : 001
- 5 : 101
- 6 : 011
- 7 : 111
(3) for i & (1 << j)
- 0 : 000 & 100/010/001 =>
- 1 : 100 & 100/010/001 => [1,]
- 2 : 010 & 100/010/001 => [2,]
- 3 : 110 & 100/010/001 => [1,2]
- 4 : 001 & 100/010/001 => [3,]
- 5 : 101 & 100/010/001 => [1,3]
- 6 : 011 & 100/010/001 => [2,3]
- 7 : 111 & 100/010/001 => [1,2,3]