
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
- Python 3 Advanced Tutorial
- Python 3 - Classes/Objects
- Python 3 - Reg Expressions
- Python 3 - CGI Programming
- Python 3 - Database Access
- Python 3 - Networking
- Python 3 - Sending Email
- Python 3 - Multithreading
- Python 3 - XML Processing
- Python 3 - GUI Programming
- Python 3 - Further Extensions
- Python 3 Useful Resources
- Python 3 - Questions and Answers
- Python 3 - Quick Guide
- Python 3 - Tools/Utilities
- Python 3 - Useful Resources
- Python 3 - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python 3 - Bitwise Operators Example
The following Bitwise operators are 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 << 2 = 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 >> 2 = 15 (means 0000 1111) |
Example
#!/usr/bin/python3 a = 60 # 60 = 0011 1100 b = 13 # 13 = 0000 1101 print ('a=',a,':',bin(a),'b=',b,':',bin(b)) c = 0 c = a & b; # 12 = 0000 1100 print ("result of AND is ", c,':',bin(c)) c = a | b; # 61 = 0011 1101 print ("result of OR is ", c,':',bin(c)) c = a ^ b; # 49 = 0011 0001 print ("result of EXOR is ", c,':',bin(c)) c = ~a; # -61 = 1100 0011 print ("result of COMPLEMENT is ", c,':',bin(c)) c = a << 2; # 240 = 1111 0000 print ("result of LEFT SHIFT is ", c,':',bin(c)) c = a >> 2; # 15 = 0000 1111 print ("result of RIGHT SHIFT is ", c,':',bin(c))
Output
When you execute the above program it produces the following result −
a = 60 : 0b111100 b = 13 : 0b1101 result of AND is 12 : 0b1100 result of OR is 61 : 0b111101 result of EXOR is 49 : 0b110001 result of COMPLEMENT is -61 : -0b111101 result of LEFT SHIFT is 240 : 0b11110000 result of RIGHT SHIFT is 15 : 0b111
python_basic_operators.htm
Advertisements