Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 06

Bitwise Operators

Work on integers bit by bit with <<, >>, &, |, ^, and ~. Floats are not allowed; the same symbols also mean set operations.

Beginner22 min

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.

Bitwise AND on 10 and 4 yields 0

What you'll learn

  • The six bitwise operators
  • Why floats raise TypeError
  • Difference between & and and
  • Bitwise operations on sets
  • Why ~n = -(n + 1)

How bitwise operators work

Python follows three steps:

  1. Convert the integer to binary
  2. Apply the bitwise operation
  3. Convert the result back to decimal

Example:

text
10  →  1010₂
4   →  0100₂
----------------
AND →  0000₂
Result = 0

Shift 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.

python
print(10 << 3)   # 80
print(4 << 3)    # 32
print(9 << 2)    # 36
print(10 << 0)   # 10

Invalid:

python
# 10.3 << 2   → TypeError
# 4 << -1     → ValueError

Right shift (>>)

Right shift removes bits from the right.

python
print(10 >> 3)   # 1
print(16 >> 2)   # 4
print(32 >> 3)   # 4
print(32 >> 0)   # 32

For 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
python
print(10 & 4)      # 0
print(5 & 4)       # 4

print(10 and 4)    # 4  — logical and, not bitwise
python
# 10.2 & 2   → TypeError

& is bitwise. and is logical — they solve different problems.

OR (|)

OR returns 1 if either bit is 1.

python
print(10 | 4)    # 14

Binary: 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
python
print(2 ^ 3)      # 1
print(10 ^ 15)    # 5

Complement (~)

Complement flips every bit. The formula to memorize:

text
~n = -(n + 1)
python
print(~10)       # -11
print(~100)      # -101
print(~(-123))   # 122

Bitwise operators on sets

The same symbols have a different meaning for sets:

python
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
python
print(10 & 4)      # 0
print(10 and 4)    # 4

The results differ because the operators answer different questions.

Practice exercises

Exercise 1 — Predict before running:

python
10 << 3
10 >> 3

Which multiplies by 8? Which divides by 8 (for this value)?

Exercise 2 — Compare:

python
10 & 4
10 and 4

Explain why the outputs differ.

Exercise 3 — Use the formula:

python
~25

Calculate manually with -(n + 1) before checking in Python.

Quick knowledge check

  1. Can you use bitwise shift on a float?
    No. It raises TypeError.

  2. What is the complement formula?
    ~n = -(n + 1)

  3. Is & the same as and?
    No. & is bitwise (or set intersection); and is 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…