Nearby lessons
34 of 159Python - Bitwise Operators
📌 What You Will Learn
- Define bitwise operators and how they work on binary values
- Use the bitwise AND, OR, and XOR operators
- Apply the bitwise complement operator with its formula
- Use the left shift and right shift operators
- Distinguish logical operators from 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
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
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
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
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
Explanation
~4
= -(4 + 1)
= -5
Another Example - Bitwise Complement
5. Left Shift Operator (<<)
The left shift operator shifts bits toward the left.
Formula:
x << n = x * 2ⁿ
Example - Left Shift
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
Explanation
10 // 2²
10 // 4
= 2
Binary Explanation - Right Shift
10 = 1010
After shifting 2 positions:
10
Decimal = 2
Bitwise Operators with Variables
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: &, | |
📝 Key Takeaways
- Bitwise operators perform operations on binary values bit by bit
- The & operator returns 1 only when both bits are 1
- The | operator returns 1 when at least one bit is 1
- The complement formula is ~x = -(x + 1)
- Left shift multiplies by 2ⁿ and right shift divides by 2ⁿ
- Bitwise operators are used in networking, cryptography, and image processing
🧠 Test Your Knowledge
8 QuestionsProgress: 0 / 8