Python Bitwise Operators
1. What are Bitwise Operators?
Bitwise operators work with binary numbers (0 and 1).
In simple words:
👉 They work on bits, not normal numbers.
These operators are mostly used in:
-
Low-level programming
-
Performance-related code
-
Special calculations
Beginners should just understand the idea.
2. Binary Numbers (Very Simple)
Binary uses only:
-
0 -
1
Example:
-
5in binary =101 -
3in binary =011
3. List of Bitwise Operators
| Operator | Name | Meaning |
|---|---|---|
& |
AND | Both bits must be 1 |
|
OR | |
^ |
XOR | Bits are different |
~ |
NOT | Reverse bits |
<< |
Left Shift | Move bits left |
>> |
Right Shift | Move bits right |
4. Bitwise AND (&)
& compares bits and returns 1 only if both bits are 1.
Example
Output:
5. Bitwise OR (|)
| returns 1 if any bit is 1.
Example
Output:
6. Bitwise XOR (^)
^ returns 1 if bits are different.
Example
Output:
7. Bitwise NOT (~)
~ reverses bits.
Example
Output:
👉 Result is negative because of Python’s internal system.
8. Bitwise Left Shift (<<)
Moves bits to the left.
Example
Output:
Explanation
5 << 1 means:
-
Multiply by 2
9. Bitwise Right Shift (>>)
Moves bits to the right.
Example
Output:
Explanation
10 >> 1 means:
-
Divide by 2
10. Simple Real-Life Understanding
| Operation | Effect |
|---|---|
<< 1 |
Multiply by 2 |
>> 1 |
Divide by 2 |
11. Common Beginner Mistakes
❌ Confusing Logical and Bitwise
They are not the same.
❌ Expecting Normal Math
12. Simple Practice Examples
Example 1: Check Even or Odd
Output:
Example 2: Double a Number
Example 3: Half a Number
13. Summary (Bitwise Operators)
✔ Work with binary (0,1)
✔ Used for special tasks
✔ & | ^ ~ << >> are bitwise operators
✔ << doubles number
✔ >> halves number
📘 Perfect for Beginner eBook
This chapter is ideal for:
-
Python beginner books
-
Students who want basics
-
Self-learners
If you want next, I can write:
-
Python Operator Exercises
-
if–else statements
-
Loops (for, while)
-
MCQs with answers
Just tell me 😊
