Python_ Bitwise operators

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.