Bitwise Operator
Bitwise operators are used for manipulating a data at the bit level, also called as bit level programming. Bit-level programming mainly consists of 0 and 1.
Operator | Description | Example |
---|---|---|
& Binary AND | Operator copies a bit to the result if it exists in both operands | a & b |
| Binary OR | It copies a bit if it exists in either operand | a | b |
^ Binary XOR | It copies the bit if it is set in one operand but not both | a ^ b |
~ Binary Ones Complement | It is unary and has the effect of 'flipping' bits | ~a |
<< Binary Left Shift | The left operands value is moved left by the number of bits specified by the right operand | a <<1 |
>> Binary Right Shift | The left operands value is moved right by the number of bits specified by the right operand | a >> 2 |
Bitwise Operator - Program Link
# Python Bitwise Operatora=int(input())b=int(input())print("Bitwise AND ",a&b)print("Bitwise OR",a|b)print("Bitwise XOR",a^b)print("Bitwise Not",~a)print("Bitwise Left Shift 1:",a<<1)print("Bitwise Right Shift 1:",a>>1)
Output
Bitwise AND 0Bitwise OR 6Bitwise XOR 6Bitwise Not -5Bitwise Left Shift 1: 8Bitwise Right Shift 1: 2
References
- Allen B. Downey, “Think Python: How to Think Like a Computer Scientist‘‘, 2nd edition, Updated for Python 3, Shroff/O‘Reilly Publishers, 2016 (http://greenteapress.com/wp/thinkpython/)
- Guido van Rossum and Fred L. Drake Jr, ―An Introduction to Python – Revised and updated for Python 3.2, Network Theory Ltd., 2011.
- John V Guttag, ―Introduction to Computation and Programming Using Python‘‘, Revised and expanded Edition, MIT Press , 2013
- Robert Sedgewick, Kevin Wayne, Robert Dondero, ―Introduction to Programming in Python: An Inter-disciplinary Approach, Pearson India Education Services Pvt. Ltd., 2016.
- Timothy A. Budd, ―Exploring Python‖, Mc-Graw Hill Education (India) Private Ltd.,, 2015. 4. Kenneth A. Lambert, ―Fundamentals of Python: First Programs‖, CENGAGE Learning, 2012.
- Charles Dierbach, ―Introduction to Computer Science using Python: A Computational Problem-Solving Focus, Wiley India Edition, 2013.
- Paul Gries, Jennifer Campbell and Jason Montojo, ―Practical Programming: An Introduction to Computer Science using Python 3‖, Second edition, Pragmatic Programmers, LLC, 2013.