비트연산
http://ko.wikipedia.org/wiki/%EB%B9%84%ED%8A%B8_%EC%97%B0%EC%82%B0
+ bit operation
print 5 >> 4 # Right Shift - 0 print 5 << 1 # Left Shift - 10 print 8 & 5 # Bitwise AND - 0 print 9 | 4 # Bitwise OR - 13 print 12 ^ 42 # Bitwise XOR - 38 print ~88 # Bitwise NOT - -89
+ lesson I0: the base 2 number system
python 에서의 2진수 표현은 0b 로 시작
print 0b1, #1 print 0b10, #2 print 0b11, #3 print 0b100, #4 print 0b101, #5 print 0b110, #6 print 0b111, #7 print 0b1000, #8 print 0b1001 #9 print "******" print 0b1 + 0b11 print 0b11 * 0b11
+ I can count to 1100!!
one = 0b1 two = 0b10 three = 0b11 four = 0b100 five = 0b101 six = 0b110 seven = 0b111 eight = 0b1000 nine = 0b1001 ten = 0b1010 eleven = 0b1011 twelve = 0b1100
+ The bin() function
hex() 16진수
oct() 8진수
bin() 2진수
print bin(1) print bin(2) print bin(3) print bin(4) print bin(5)
+ int()’s second parameter
print int("1",2)
print int("10",2)
print int("111",2)
print int("0b100",2)
print int(bin(5),2)
# Print out the decimal equivalent of the binary 11001001.
print int("11001001", 2)
+ Slide to the Left! Slide to the Right
shift_right = 0b1100 shift_left = 0b1 # Your code here! shift_right = shift_right >> 2 shift_left = shift_left << 2 print bin(shift_right) print bin(shift_left)
+ A BIT of This AND That
# 0 & 0 = 0 # 0 & 1 = 0 # 1 & 0 = 0 # 1 & 1 = 1 print bin(0b1110 & 0b101)
+ A BIT of This OR That
# 0 | 0 = 0 # 0 | 1 = 1 # 1 | 0 = 1 # 1 | 1 = 1 print bin(0b1110 | 0b101)
+ This XOR That
# 0 ^ 0 = 0 # 0 ^ 1 = 1 # 1 ^ 0 = 1 # 1 ^ 1 = 0 print bin(0b1110 ^ 0b101)