python学习笔记(3)—— 进制

  python中使用二进制也很方便

  1、表示

1 one = 0b1
2 two = 0b10
3 three = 0b11

  2、操作

1 print 5 >> 4  # Right Shift
2 print 5 << 1  # Left Shift
3 print 8 & 5   # Bitwise AND
4 print 9 | 4   # Bitwise OR
5 print 12 ^ 42 # Bitwise XOR
6 print ~88     # Bitwise NOT

  3、转换

1 print int("0b100",2)
2 print int(bin(5),2)
3 print int("11001001",2)
1 print bin(1)
2 print bin(2)
3 print bin(3)

 

posted @ 2014-11-20 17:59  andrew.elec90  阅读(128)  评论(0)    收藏  举报