解析Python中的二进制位运算符

小鱼们欢快的闹腾起来,有的甩动着尾巴,有的吐着泡泡,有的扭动着身体,互相追玩,好不热闹。春天来了!你看,融化的冰水把小溪弄醒了。 "丁粳、丁粳 ",它就像大自然的神奇歌手,唱着清脆悦耳的歌,向前奔流……

下表列出了所有的Python语言的支持位运算符。假设变量a持有60和变量b持有13,则:

示例:

试试下面的例子就明白了所有的Python编程语言提供了位运算符:

#!/usr/bin/python

a = 60      # 60 = 0011 1100 
b = 13      # 13 = 0000 1101 
c = 0

c = a & b;    # 12 = 0000 1100
print "Line 1 - Value of c is ", c

c = a | b;    # 61 = 0011 1101 
print "Line 2 - Value of c is ", c

c = a ^ b;    # 49 = 0011 0001
print "Line 3 - Value of c is ", c

c = ~a;      # -61 = 1100 0011
print "Line 4 - Value of c is ", c

c = a << 2;    # 240 = 1111 0000
print "Line 5 - Value of c is ", c

c = a >> 2;    # 15 = 0000 1111
print "Line 6 - Value of c is ", c

当执行上面的程序它会产生以下结果:

Line 1 - Value of c is 12
Line 2 - Value of c is 61
Line 3 - Value of c is 49
Line 4 - Value of c is -61
Line 5 - Value of c is 240
Line 6 - Value of c is 15

本文解析Python中的二进制位运算符到此结束。人生如棋,仅有舍弃一些棋子,才能换得一生的平静和幸福。上帝是公平的,冬天里失去的东西,到春天的时候会还给你;这方面舍弃的东西,在另一方面会补给你。舍与得,其实是一个无限循环的圈。越敢舍,越能得;越愿舍,越易得。舍得之间,人生迥异,以舍为得,妙用无穷!小编再次感谢大家对我们的支持!

标签: 运算符 Python