以下实例演示了Python所有位运算符的操作:
实例(Python 2.0+)
#!/usr/bin/python# -*- coding: UTF-8 -*-a = 60 # 60 = 0011 1100 b = 13 # 13 = 0000 1101 c = 0c = a & b; # 12 = 0000 1100print"1 - c 的值为:", cc = a | b; # 61 = 0011 1101 print"2 - c 的值为:", cc = a ^ b; # 49 = 0011 0001print"3 - c 的值为:", cc = ~a; # -61 = 1100 0011print"4 - c 的值为:", cc = a << 2; # 240 = 1111 0000print"5 - c 的值为:", cc = a >> 2; # 15 = 0000 1111print"6 - c 的值为:", c
以上实例输出结果:
1- c 的值为:12
2- c 的值为:61
3- c 的值为:49
4- c 的值为:-61
5- c 的值为:240
6- c 的值为:15