Source: Http://www.runoob.com/python/python-operators.html#ysf5
1, bitwise operators
Convert to binary one-to-one operation
& |
Bitwise-AND |
1&1 is 1 |
| |
Bitwise OR |
1|0 or 0|1 or 1|1 is 1 |
^ |
Bitwise XOR OR |
0^1 or 1^0 is 1 |
~ |
Bitwise REVERSE |
Take counter |
<< |
Move left |
"<<" The left of the binary number of all left a number of bits, the "<<" to the right of the number of digits specified by the number of moves, high drop, low 0. |
>> |
Move right |
Shift all the binary of the left operand of ">>" to the right of several bits, and the number to the right of ">>" to specify the number of bits to move |
1A = 60#= 0011 11002b = 13#= 0000 11013c =04 5c = A & B;#0000 11006 Print(c)7 8c = A | b#1101 = 00119 Print(c)Ten Onec = a ^ b;#0001 = 0011 A Print(c) - -c = ~a;#-61 = 1100 0011 the Print(c) - -c = a << 2;#0000 = 1111 - Print(c) + -c = a >> 2;#= 0000 1111 + Print(c)
View Code
2. Member operators
Inch |
X in Y, if X is in the y sequence, returns True |
Not in |
X not in y,x does not return True in the y sequence |
1A = 102b = 203list = [1, 2, 3, 4, 5 ];4 5 if(Ainchlist):6 Print("1-Variable A in the list given")7 Else:8 Print("1-Variable A is not in the list given")#9 Ten if(b not inchlist): One Print("2-Variable B is not in the list given") A Else: - Print("2-Variable B in the list given")# - the #Modify the value of variable a -A = 2 - if(Ainchlist): - Print("3-Variable A in the list given")# + Else: - Print("3-Variable A is not in the list given")
View Code
3. Identity operator
Is |
Determines whether two identifiers are referenced from an object, X is Y, if the ID (x) equals ID (y), returns the result 1 |
is not |
Determines whether the reference is from a different object, x is not y, and if the ID (x) does not equal the ID (Y), returns the result 1 |
1A = 202b = 203 4 if(A isb):5 Print("1-a and B have the same identity")6 Else:7 Print("1-a and B do not have the same identity")8 9 if(ID (a) = =ID (b)):Ten Print("2-a and B have the same identity") One Else: A Print("2-a and B do not have the same identity") - - #Modify the value of variable B theb = 30 - if(A isb): - Print("3-a and B have the same identity") - Else: + Print("3-a and B do not have the same identity") - + if(A is notb): A Print("4-a and B do not have the same identity") at Else: - Print("4-a and B have the same identity") - - - Output: ->>> in1-A and B have the same identity -2-A and B have the same identity to3-A and B do not have the same identity +4-A and B do not have the same identity ->>>
View Code
Python_ Bitwise operators