Nearby lessons
39 of 108Python - Bitwise Operators
Introduction
Bitwise operators are used to perform operations at the bit level.
These operators work on binary values (0 and 1).
What are Bitwise Operators?
Before performing a bitwise operation, Python converts decimal numbers into binary form.
The operation is then performed bit by bit.
Bitwise Operators List
| Operator | Meaning |
|---|---|
& | Bitwise AND |
| | Bitwise OR |
^ | Bitwise XOR |
~ | Bitwise Complement |
<< | Left Shift |
>> | Right Shift |
Understanding Binary Values
Consider the following example:
| Decimal | Binary |
|---|---|
| 4 | 100 |
| 5 | 101 |
Example - Binary Values
🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
Binary of 4 = 100 Binary of 5 = 101
1. Bitwise AND Operator (&)
The & operator returns 1 only when both bits are 1.
Otherwise, it returns 0.
Truth Table - AND Operator
| Bit 1 | Bit 2 | Result |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
Example - Bitwise AND
🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
4
Explanation
4 = 100
5 = 101
100
101
---
100
Binary 100 = Decimal 4
2. Bitwise OR Operator (|)
The | operator returns 1 if at least one bit is 1.
Truth Table - OR Operator
| Bit 1 | Bit 2 | Result |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |
Example - Bitwise OR
🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
5
Explanation
4 = 100
5 = 101
100
101
---
101
Binary 101 = Decimal 5
3. Bitwise XOR Operator (^)
The ^ operator returns 1 only when both bits are different.
Truth Table - XOR Operator
| Bit 1 | Bit 2 | Result |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
Example - Bitwise XOR
🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
1
Explanation
4 = 100
5 = 101
100
101
---
001
Binary 001 = Decimal 1
4. Bitwise Complement Operator (~)
The ~ operator is also called the Bitwise NOT operator.
Formula:
~x = -(x + 1)
Example - Bitwise Complement
🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
-5
Explanation
~4
= -(4 + 1)
= -5
Another Example - Bitwise Complement
🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
-11
5. Left Shift Operator (<<)
The left shift operator shifts bits toward the left.
Formula:
x << n = x * 2ⁿ
Example - Left Shift
🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
40
Explanation
10 * 2²
10 * 4
= 40
Binary Explanation - Left Shift
10 = 1010
After shifting 2 positions:
101000
Decimal = 40
6. Right Shift Operator (>>)
The right shift operator shifts bits toward the right.
Formula:
x >> n = x // 2ⁿ
Example - Right Shift
🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
2
Explanation
10 // 2²
10 // 4
= 2
Binary Explanation - Right Shift
10 = 1010
After shifting 2 positions:
10
Decimal = 2
Bitwise Operators with Variables
🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
4 5 1
Important Notes
- Bitwise operators work on binary values.
- They are mostly used in low-level programming.
- Shift operators are faster than multiplication and division in many low-level operations.
- The complement operator always returns a negative value.
Real World Usage of Bitwise Operators
Bitwise operators are commonly used in:
- Networking
- Cryptography
- Embedded systems
- Operating systems
- Image processing
Example Areas
| Operator | Usage |
|---|---|
& | Masking |
| | Setting bits |
^ | Toggling bits |
<< | Fast multiplication |
>> | Fast division |
Difference Between Logical and Bitwise Operators
| Logical Operators | Bitwise Operators |
|---|---|
| Work on Boolean values. | Work on binary bits. |
Examples: and, or |
Examples: &, | |
Quick Summary
| Operator | Purpose |
|---|---|
& | Bitwise AND |
| | Bitwise OR |
^ | Bitwise XOR |
~ | Bitwise Complement |
<< | Left Shift |
>> | Right Shift |
🧠 Test Your Knowledge
8 QuestionsProgress: 0 / 8
Keep Going!Python - Identity Operators