#32-파이썬 기초 실습 - 비트연산자

2020. 3. 14. 09:03AI & BigData/Python Basics

파이썬 기초 실습 - 비트연산자

비트 연산자

No 연산자 설명
1 & 비트 AND
2    
3 ^ 비트 XOR(배타적 OR, Exclusive OR
4 ~ 비트 NOT
5 << 비트를 왼쪽으로 시프트
6 >> 비트를 오른쪽으로 시프트
7 &= 비트를 AND 연산 후 할당
8   =
9 ^= 비트 XOR 연산 후 할당
10 <<= 비트를 왼쪽으로 시프트한 후 할당
11 >>= 비트를 오른쪽으로 시프트 한 후 할당
# 참
True
True
# 거짓
False
False
#  1
# 비트 AND 연산
print(0 & 0)
print(0 & 1)
print(1 & 0)
print(1 & 1)

print(False & False)
print(False & True)
print(True & False)
print(True & True)
0
0
0
1
False
False
False
True
# 2
# 비트 OR 연산
print(0 | 0)
print(0 | 1)
print(1 | 0)
print(1 | 1)

print(False | False)
print(False | True)
print(True | False)
print(True | True)
0
1
1
1
False
True
True
True
# 3
# 비트 XOR(배타적 OR, Exclusive OR)
print(0 ^ 0)
print(0 ^ 1)
print(1 ^ 0)
print(1 ^ 1)

print(False ^ False)
print(False ^ True)
print(True ^ False)
print(True ^ True)
0
1
1
0
False
True
True
False
# 4 
# 비트 NOT
print(~0)
print(~1)
print(~False)
print(~True)
-1
-2
-1
-2
# 5
# 비트를 왼쪽으로 시프트
1 << 2
4
# 6 
# 비트를 오른쪽으로 시프트
10 >> 2
2
# 7
# 비트를 AND 연산 후 할당
data = 1
data &= 1
data
1
# 8
# 비트를 OR 연산 후 할당
data = 1
data |= 1
data
1
# 9
# 비트를 XOR 연산 후 할당
data = 1
data ^= 1
data
0
# 10
# 비트를 왼쪽으로 시프트한 후 할당
data = 1
data <<= 1
data
2
# 11
# 비트를 오른쪽으로 시프트 한 후 할당
data = 10
data >>= 1
data
5