Lesson 06
Bitwise Operators
Work on integers bit by bit with <<, >>, &, |, ^, and ~. Floats are not allowed; the same symbols also mean set operations.
Bitwise operators work on integers bit by bit. Python converts each integer to binary, performs the operation, and returns the result as a decimal integer.
Important: Bitwise operators do not work on floats.
What you'll learn
- The six bitwise operators
- Why floats raise
TypeError - Difference between
&andand - Bitwise operations on sets
- Why
~n = -(n + 1)
How bitwise operators work
Python follows three steps:
- Convert the integer to binary
- Apply the bitwise operation
- Convert the result back to decimal
Example:
10 → 1010₂
4 → 0100₂
----------------
AND → 0000₂
Result = 0Shift explorer
Each left shift by 1 multiplies by 2; right shift divides (for positive integers).
Decimal 10
Binary 00001010
10 << 3 = 80
Result binary 01010000
AND, OR, XOR
Compare bitwise & with logical and.
A (bin) 00001010
B (bin) 00000100
10 & 4 = 0
10 and 4 = 4 (logical — different operator)
10 | 4 = 14 · 10 ^ 4 = 14
Complement (~)
~10 = -11
Formula: -(10 + 1) = -11
Sets (same symbols)
s1 = {10, 20, 30} · s2 = {15, 20, 35}
- s1 & s2 = {20}
- s1 | s2 = {10, 15, 20, 30, 35}
- s1 ^ s2 = {10, 15, 30, 35}
The six bitwise operators
| Symbol | Name | Purpose |
|---|---|---|
<< |
Left shift | Move bits left |
>> |
Right shift | Move bits right |
& |
AND | Both bits must be 1 |
| |
OR | Either bit is 1 |
^ |
XOR | Bits are different |
~ |
Complement | Invert every bit |
Left shift (<<)
Left shift moves bits left and fills zeros on the right.
Each left shift by 1 multiplies by 2. Example: 10 << 3 = 80.
print(10 << 3) # 80
print(4 << 3) # 32
print(9 << 2) # 36
print(10 << 0) # 10Invalid:
# 10.3 << 2 → TypeError
# 4 << -1 → ValueErrorRight shift (>>)
Right shift removes bits from the right.
print(10 >> 3) # 1
print(16 >> 2) # 4
print(32 >> 3) # 4
print(32 >> 0) # 32For positive integers, >> 3 is like dividing by 8 (when bits do not get lost).
AND (&)
AND returns 1 only when both bits are 1.
| A | B | A & B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
print(10 & 4) # 0
print(5 & 4) # 4
print(10 and 4) # 4 — logical and, not bitwise# 10.2 & 2 → TypeError& is bitwise. and is logical — they solve different problems.
OR (|)
OR returns 1 if either bit is 1.
print(10 | 4) # 14Binary: 00001010 | 00000100 = 00001110
XOR (^)
XOR returns 1 when bits are different.
| A | B | A ^ B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
print(2 ^ 3) # 1
print(10 ^ 15) # 5Complement (~)
Complement flips every bit. The formula to memorize:
~n = -(n + 1)print(~10) # -11
print(~100) # -101
print(~(-123)) # 122Bitwise operators on sets
The same symbols have a different meaning for sets:
s1 = {10, 20, 30}
s2 = {15, 20, 35}
print(s1 & s2) # {20} intersection
print(s1 | s2) # {10, 15, 20, 30, 35} union
print(s1 ^ s2) # {10, 15, 30, 35} symmetric difference& vs and
| Operator | Purpose | Example |
|---|---|---|
& |
Bitwise / set intersection | 10 & 4 → 0 |
and |
Logical operator | 10 and 4 → 4 |
print(10 & 4) # 0
print(10 and 4) # 4The results differ because the operators answer different questions.
Practice exercises
Exercise 1 — Predict before running:
10 << 3
10 >> 3Which multiplies by 8? Which divides by 8 (for this value)?
Exercise 2 — Compare:
10 & 4
10 and 4Explain why the outputs differ.
Exercise 3 — Use the formula:
~25Calculate manually with -(n + 1) before checking in Python.
Quick knowledge check
-
Can you use bitwise shift on a float?
No. It raisesTypeError. -
What is the complement formula?
~n = -(n + 1) -
Is
&the same asand?
No.&is bitwise (or set intersection);andis logical.
Summary
- Bitwise operators work on integers (not floats).
- Python provides six operators:
<<,>>,&,|,^, and~. - Shifts reject floats and negative shift counts.
&,|, and^also work with sets as intersection, union, and symmetric difference.- Memorize
~n = -(n + 1).
Next lesson: Membership Operators (in / not in) — check whether values exist inside strings, lists, dictionaries, and sets.
Loading playground…