After completing this chapter, you will be able to:
- Understand Bitwise Operators.
- Explain why bitwise operators are used.
- Learn binary representation of numbers.
- Perform bitwise operations.
- Write programs using bitwise operators.
- Understand left shift and right shift operations.
Bitwise Operators perform operations directly on the binary representation (bits) of numbers.
Every integer inside a computer is stored in the form of 0s and 1s.
Bitwise operators manipulate these bits to produce a new result.
They are mainly used in:
- System Programming
- Embedded Systems
- Networking
- Cryptography
- Device Drivers
- Performance Optimization
A Bitwise Operator performs operations on the binary representation of integer values.
Python provides six bitwise operators.
Bitwise operators help to:
- Perform binary calculations.
- Manipulate bits.
- Optimize memory usage.
- Improve execution speed.
- Work with hardware devices.
Before learning bitwise operators, understand binary numbers.
Example
Decimal Binary
5 0101
3 0011
| Operator | Name |
|---|---|
| & | Bitwise AND |
| | | Bitwise OR |
| ^ | Bitwise XOR |
| ~ | Bitwise NOT |
| << | Left Shift |
| >> | Right Shift |
Returns 1 only if both corresponding bits are 1.
5 = 0101
3 = 0011
--------------
& = 0001
--------------
Result = 1
a = 5
b = 3
print(a & b)1
Returns 1 if at least one corresponding bit is 1.
5 = 0101
3 = 0011
--------------
| = 0111
--------------
Result = 7
a = 5
b = 3
print(a | b)7
Returns 1 only when corresponding bits are different.
5 = 0101
3 = 0011
--------------
^ = 0110
--------------
Result = 6
a = 5
b = 3
print(a ^ b)6
Reverses every bit of the number.
number = 5
print(~number)-6
Python calculates
~n = -(n + 1)
Therefore,
~5 = -(5 + 1)
= -6
Moves all bits towards the left by the specified number of positions.
Each left shift multiplies the value by 2.
5 = 00000101
5 << 1
00001010
Result = 10
print(5 << 1)10
Moves all bits towards the right.
Each right shift divides the value by 2.
20 = 00010100
20 >> 2
00000101
Result = 5
print(20 >> 2)5
Write a Python program to demonstrate all Bitwise Operators.
a = 5
b = 3
print("AND :", a & b)
print("OR :", a | b)
print("XOR :", a ^ b)
print("NOT :", ~a)
print("LEFT SHIFT :", a << 1)
print("RIGHT SHIFT :", a >> 1)AND : 1
OR : 7
XOR : 6
NOT : -6
LEFT SHIFT : 10
RIGHT SHIFT : 2
START
Create two variables
Perform all bitwise operations
Display the results
STOP
Bitwise operators are used in:
- Operating Systems
- Embedded Systems
- Computer Networks
- Image Processing
- Encryption Algorithms
- Device Drivers
- Faster execution.
- Efficient memory usage.
- Useful in low-level programming.
- Direct manipulation of binary data.
| Logical Operators | Bitwise Operators |
|---|---|
| Work with Boolean values | Work with binary bits |
| Return True or False | Return integer values |
| Used in conditions | Used for bit manipulation |
❌ Incorrect
if True & False:
print("Hello")Although this works with Boolean values, logical operators (and, or, not) should be used for conditions.
5 = 0101
3 = 0011
OR = 0111 = 7
XOR = 0110 = 6
Remember:
- OR returns 1 if either bit is 1.
- XOR returns 1 only if the bits are different.
Bitwise operators perform operations on the binary representation of integers.
Six:
- &
- |
- ^
- ~
- <<
-
It returns 1 only when both corresponding bits are 1.
It shifts bits to the left and effectively multiplies the value by 2 for each shift position.
Python evaluates the bitwise NOT using the formula:
~n = -(n + 1)
Therefore,
~5 = -6
&→ Bitwise AND|→ Bitwise OR^→ Bitwise XOR~→ Bitwise NOT<<→ Left Shift>>→ Right Shift- Bitwise operators work on binary numbers.
In this chapter, you learned:
- What Bitwise Operators are.
- Binary representation.
- Types of Bitwise Operators.
- Practical examples.
- Real-world applications.
- Difference between logical and bitwise operators.
- Common mistakes.
- Interview questions.