[Algo] 비트연산

AOD·2023년 6월 12일
0

Algorithm

목록 보기
4/31
post-thumbnail

비트연산

arr = [1,2,3]

n = len(arr)

for i in range(1<<n):
    print(i,end = ' ')
    for j in range(n): # 012
        if i & (1<<j):
            print(arr[j], end = ',')
    print()
print()

(1) (1<<n)

  • n = 3, 1 -> 0001

(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]
profile
No end point for Growth. 2023.01.02 ~ SoftWare공부 시작

0개의 댓글