解析Python中的二進制位運算符
更新時間:2015年05月13日 12:16:27 投稿:goldensun
這篇文章主要介紹了解析Python中的二進制位運算符,是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
當執(zhí)行上面的程序它會產生以下結果:
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 cookbook(數據結構與算法)找出序列中出現次數最多的元素算法示例
這篇文章主要介紹了Python cookbook(數據結構與算法)找出序列中出現次數最多的元素算法,涉及Python collections模塊中的Counter類相關使用技巧與操作注意事項,需要的朋友可以參考下2018-03-03