Home Course Roadmap
Home / Python Foundations / Bitwise Operators
Lesson 14 · Operators

Python Bitwise Operators

Learn how Python works with individual bits using bitwise AND, OR, XOR, NOT, left shift, and right shift operators.

35–45 minutes
Beginner
Python Foundations
Reading progress 0%

What You'll Learn

  • Understand binary representation of integers.
  • Learn all six major bitwise operators.
  • Understand how bitwise operations work at the binary level.
  • Use bitwise operations in Python programs.
  • Understand left and right shifts.
  • Avoid common bitwise programming mistakes.
01

What Are Bitwise Operators?

Bitwise operators work directly with the individual binary bits that make up an integer.

While arithmetic operators work with numbers as mathematical values, bitwise operators work with their binary representation.

Python Bitwise Operators
&     Bitwise AND
|     Bitwise OR
^     Bitwise XOR
~     Bitwise NOT
<<    Left Shift
>>    Right Shift

Why Learn Bitwise Operators?

Bitwise operations are useful in areas such as low-level programming, networking, permissions, flags, embedded systems, compression, encryption techniques, and performance-sensitive programs.

02

Understanding Binary

Computers represent data using binary digits, also called bits.

A bit can have only two possible values: 0 or 1.

For example, the decimal number 5 can be represented in binary as:

Decimal: 5
Binary:  0101

The binary positions represent powers of two:

8   4   2   1
0   1   0   1

Therefore:

0 × 8
1 × 4
0 × 2
1 × 1

= 5

Using bin()

Python provides the bin() function to display an integer in binary form.

bin() Example
print(bin(5))
print(bin(10))
print(bin(15))
Output
0b101
0b1010
0b1111

The prefix 0b tells Python that the number is represented in binary.

03

Bitwise AND &

The bitwise AND operator compares two bits. The result is 1 only when both bits are 1.

1 & 1 = 1
1 & 0 = 0
0 & 1 = 0
0 & 0 = 0

Example

Bitwise AND
a = 5
b = 3

print(a & b)

In binary:

5 = 0101
3 = 0011

    0101
&   0011
---------
    0001

Result = 1
Output
1
04

Bitwise OR |

The bitwise OR operator produces 1 when at least one of the corresponding bits is 1.

1 | 1 = 1
1 | 0 = 1
0 | 1 = 1
0 | 0 = 0

Example

Bitwise OR
a = 5
b = 3

print(a | b)

Binary calculation:

5 = 0101
3 = 0011

    0101
|   0011
---------
    0111

Result = 7
Output
7
05

Bitwise XOR ^

XOR stands for Exclusive OR. It produces 1 when the two bits are different.

1 ^ 1 = 0
1 ^ 0 = 1
0 ^ 1 = 1
0 ^ 0 = 0

Example

Bitwise XOR
a = 5
b = 3

print(a ^ b)

Binary calculation:

5 = 0101
3 = 0011

    0101
^   0011
---------
    0110

Result = 6
Output
6
06

Bitwise NOT ~

The bitwise NOT operator flips the bits of an integer. Every 0 becomes 1, and every 1 becomes 0.

In Python, integers use signed representation, so understanding the mathematical result is important.

Bitwise NOT
x = 5

print(~x)
Output
-6

A useful rule for Python integers is:

~x = -(x + 1)

Therefore:

~5
= -(5 + 1)
= -6
07

Left Shift <<

The left shift operator moves the binary bits to the left by a specified number of positions.

Left Shift Example
x = 5

print(x << 1)
print(x << 2)
Output
10
20

In binary:

5 = 0101

5 << 1 = 1010 = 10

5 << 2 = 10100 = 20

Simple Rule

For positive integers, shifting left by one position is equivalent to multiplying by 2.

Shifting left by two positions is equivalent to multiplying by 4.

08

Right Shift >>

The right shift operator moves bits to the right.

Right Shift Example
x = 20

print(x >> 1)
print(x >> 2)
Output
10
5
20 = 10100

20 >> 1 = 01010 = 10

20 >> 2 = 00101 = 5

Simple Rule

For positive integers, shifting right by one position is similar to floor-dividing by 2.

Shifting right by two positions is similar to floor-dividing by 4.

09

Bitwise Operators Compared

Here is a quick reference for the six main bitwise operators:

&    AND
|    OR
^    XOR
~    NOT
<<   Left Shift
>>   Right Shift

Example with 5 and 3

a = 5
b = 3

print(a & b)
print(a | b)
print(a ^ b)
Output
1
7
6
Remember the Pattern

AND requires both bits to be 1. OR requires at least one bit to be 1. XOR requires the bits to be different.

10

Practical Bitwise Examples

Example 1 — Checking Whether a Number Is Odd

The least significant bit of an integer is 1 when the number is odd.

Odd Number Check
number = 17

if number & 1:
    print("Odd")
else:
    print("Even")
Output
Odd

Example 2 — Working with Flags

Bitwise operations can be used to store multiple yes/no settings inside a single integer.

READ = 1
WRITE = 2
DELETE = 4

permissions = READ | WRITE

print(permissions)
Output
3

The value 3 represents both READ and WRITE flags.

Checking a Flag

READ = 1
WRITE = 2
DELETE = 4

permissions = READ | WRITE

if permissions & WRITE:
    print("Write permission enabled")
Output
Write permission enabled
11

Bitwise Operators and Negative Numbers

Python's handling of negative integers can initially seem confusing when learning bitwise operations.

For example:

print(~5)
print(~10)
Output
-6
-11

This follows Python's rule:

~x = -(x + 1)
Don't Memorize Binary Pictures Alone

When working with negative integers, focus on Python's defined integer behavior rather than assuming a fixed-width representation such as exactly 8 or 32 bits.

12

Bitwise Operator Precedence

When several operators appear in the same expression, Python follows a precedence order.

Parentheses can make your intention clearer.

Using Parentheses
a = 5
b = 3
c = 1

result = (a & b) | c

print(result)
Output
1

Best Practice

If a bitwise expression becomes difficult to read, use parentheses instead of relying on the reader to remember precedence rules.

13

Common Bitwise Mistakes

Mistake 1 — Confusing & with and

These operators have completely different purposes.

# Logical AND
result = True and False

# Bitwise AND
result = 5 & 3

and works with logical truth values, while & performs a bitwise operation.

Mistake 2 — Confusing | with or

Similarly, or is a logical operator, while | is a bitwise OR operator.

Mistake 3 — Forgetting Binary Representation

Bitwise operators become much easier to understand when you write the numbers in binary.

Mistake 4 — Treating ~x as Simply -x

In Python:

~5 == -6

not -5.

14

Practice Exercises

Exercise 1 — AND

Predict the result of:

print(12 & 10)

Exercise 2 — OR

Predict the result of:

print(12 | 10)

Exercise 3 — XOR

Predict the result of:

print(12 ^ 10)

Exercise 4 — NOT

Predict the result of:

print(~7)

Exercise 5 — Left Shift

What will this produce?

print(4 << 2)

Exercise 6 — Right Shift

What will this produce?

print(32 >> 3)
15

Mini Challenge — Permission Flags

Build a small permission system using bitwise operators.

Requirements:

  • Create three permissions: READ, WRITE, and DELETE.
  • Give a user READ and WRITE permissions.
  • Use bitwise OR to combine permissions.
  • Use bitwise AND to check whether a permission is enabled.
Challenge Example
READ = 1
WRITE = 2
DELETE = 4

user_permissions = READ | WRITE

if user_permissions & READ:
    print("Read permission: YES")

if user_permissions & WRITE:
    print("Write permission: YES")

if user_permissions & DELETE:
    print("Delete permission: YES")
Output
Read permission: YES
Write permission: YES

Take It Further

Add an ADMIN permission and create a function that displays all permissions assigned to a user.

16

Quick Knowledge Check

1. Which operator performs bitwise AND?

2. Which operator performs bitwise XOR?

3. What is 5 & 3?

4. What is 5 | 3?

5. What is 5 ^ 3?

6. What is 5 << 1?

7. What is 20 >> 2?

8. What is ~5 in Python?

17

Lesson Summary

Bitwise operators allow Python programs to manipulate individual bits of integer values.

& Bitwise AND — both corresponding bits must be 1.
| Bitwise OR — at least one corresponding bit must be 1.
^ Bitwise XOR — corresponding bits must be different.
~ Bitwise NOT — flips the bits.
<< Left shift — moves bits to the left.
>> Right shift — moves bits to the right.

Key Takeaway

Bitwise operators work at the binary level. Once you understand how decimal numbers are represented using 0s and 1s, operators such as &, |, ^, ~, <<, and >> become much easier to understand.

You've Reached the End!

Excellent work! You've learned how Python performs operations at the binary level.