博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python bitwise operator
阅读量:4030 次
发布时间:2019-05-24

本文共 1823 字,大约阅读时间需要 6 分钟。

bitwise

英 [bɪt'waɪz]     
美 [bɪt'waɪz]    
  • n.逐位;按位

  1. However,you probably won't use the bitwise operators much.
    然而,也许不必过于频繁地进行按位运算。
  2. Is a numeric value representing the inclusive bitwise disjunction of two numeric bit patterns.
    是一个表示两个数位组合模式的包含按位析取的数值。

There are following Bitwise operators supported by Python language

Operator Description Example
& Binary AND Operator copies a bit to the result if it exists in both operands  (a & b) (means 0000 1100)
| Binary OR It copies a bit if it exists in either operand. (a | b) = 61 (means 0011 1101)
^ Binary XOR It copies the bit if it is set in one operand but not both. (a ^ b) = 49 (means 0011 0001)
~ Binary Ones Complement It is unary and has the effect of 'flipping' bits. (~a ) = -61 (means 1100 0011 in 2's complement form due to a signed binary number.
<< Binary Left Shift The left operands value is moved left by the number of bits specified by the right operand. a << = 240 (means 1111 0000)
>> Binary Right Shift The left operands value is moved right by the number of bits specified by the right operand. a >> = 15 (means 0000 1111)

Example

#!/usr/bin/pythona = 60            # 60 = 0011 1100 b = 13            # 13 = 0000 1101 c = 0c = a & b;        # 12 = 0000 1100print "Line 1 - Value of c is ", cc = a | b;        # 61 = 0011 1101 print "Line 2 - Value of c is ", cc = a ^ b;        # 49 = 0011 0001print "Line 3 - Value of c is ", cc = ~a;           # -61 = 1100 0011print "Line 4 - Value of c is ", cc = a << 2;       # 240 = 1111 0000print "Line 5 - Value of c is ", cc = a >> 2;       # 15 = 0000 1111print "Line 6 - Value of c is ", c

When you execute the above program it produces the following result −

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

转载地址:http://kshbi.baihongyu.com/

你可能感兴趣的文章
POJ 2656 Unhappy Jinjin(我的水题之路——不开心的学习日)
查看>>
POJ 2664 Prerequisites?(我的水题之路——选课)
查看>>
POJ 2665 Trees(我的水题之路——移树,POJ100题啦!)
查看>>
POJ 2840 Big Clock(我的水题之路——水,钟)
查看>>
POJ 2864 Pascal Library(我的水题之路——都来了么)
查看>>
POJ 2871 A Simple Question of Chemistry(我的水题之路——数列两数之差 )
查看>>
POJ 2909 Goldbach's Conjecture(我的水题之路——任一数为素数对之和)
查看>>
POJ 2924 Gauß in Elementary School(我的水题之路——n到m的连和)
查看>>
POJ 3006 Dirichlet's Theorem on Arithmetic Progressions(我的水题之路——加i个d后的第几个素数)
查看>>
POJ 3030 Nasty Hacks(我的水题之路——比较大小)
查看>>
POJ 3062 Celebrity jeopardy(我的水题之路——原样输出)
查看>>
POJ 3077 Rounders(我的水题之路——高精度四舍五入)
查看>>
POJ 3086 Triangular Sums(我的水题之路——三角数累加)
查看>>
POJ 3096 Surprising Strings(我的水题之路——重点,D-pairs)
查看>>
POJ 3100 Root of the Problem(我的水题之路——取A^N最接近B的A)
查看>>
POJ 3117 World Cup(我的水题之路——世界杯平局数目)
查看>>
POJ 3158 Kickdown(我的水题之路——齿轮盒子,读题失败)
查看>>
POJ 3183 Stump Removal(我的水题之路——高峰烧火,在线判断)
查看>>
POJ 3224 Go for Lab Cup!(我的水题之路——赢的场数最多)
查看>>
POJ 3300 Tour de France(我的水题之路——车轮角速度最大)
查看>>