W3Schools Python Bitwise Operators: Master The Bitwise Tricks

Logan Jul 13, 2026

Python, a high-level programming language known for its readability and simplicity, provides several operators to perform bitwise operations. W3Schools, a popular website for learning coding and web development, offers an excellent resource to understand and practice these operators. Let's delve into the world of bitwise operators in Python with the help of W3Schools.

Python Bitwise Operators - A proper guide with syntax and examples
Python Bitwise Operators - A proper guide with syntax and examples

Before we dive into the bitwise operators, let's quickly recap what bitwise operations are. Bitwise operations work on the bits of numbers, treating them as a sequence of binary digits – 0s and 1s. They can perform a variety of tasks, from bitwise AND to bitwise shifts, which can be invaluable in certain situations.

Python bitwise operators cheat sheet
Python bitwise operators cheat sheet

Bitwise AND (&)

The bitwise AND operator '&' returns 1 if both operands are 1, otherwise, it returns 0. It uses the binary representation of the integers. For instance, if we take two integers, 13 (1101) and 3 (0011), their bitwise AND operation will result in 1 (0001).

Bitwise Operators in Python
Bitwise Operators in Python

Here's how you can use it in Python, as shown on W3Schools:

result = 13 & 3
print(result)  # Outputs: 1

Using Bitwise AND in practice

Python bitwise operators cheat-sheet
Python bitwise operators cheat-sheet

One practical use of the bitwise AND operator is in data masking. For example, if you want to mask out certain bits, you can use the bitwise AND. Suppose you have a number 18 (10010) and you want to retain only the last three bits, you can use 'mask = 7 (000111)' and perform bitwise AND on those two numbers.

Bitwise AND as access rights

In operating systems, bitwise AND is used to check access rights for files and directories. It follows a similar principle where a read bit is 4, a write bit is 2, and an execute bit is 1. The access rights are determined by bitwise AND operations on these.

Python Operators Tutorial – Chapter 3 Notes
Python Operators Tutorial – Chapter 3 Notes

Bitwise OR (|)

The bitwise OR operator '|' returns 1 if any of the operands are 1, otherwise, it returns 0. Continuing with the example from bitwise AND, if we take the same two integers, 13 (1101) and 3 (0011), their bitwise OR operation will result in 15 (1111).

Here's how you can use it in Python:

Part 8: Bitwise Operators
Part 8: Bitwise Operators

result = 13 | 3
print(result)  # Outputs: 15

Using Bitwise OR in practice

Bitwise OR can be used when you want to combine data. For instance, suppose you have two numbers where one has certain bits set to 1 (consider it as a condition met) and the other has more conditions met, set to 1. The bitwise OR of those two numbers will give you the complete set of conditions met (those marked as 1).

Python Operators Explained for Beginners
Python Operators Explained for Beginners
Python Cheat Sheet for Beginners 2026 | Python Basics, Syntax, Loops, Functions & Variables
Python Cheat Sheet for Beginners 2026 | Python Basics, Syntax, Loops, Functions & Variables
Python Operators 😎✨
Python Operators 😎✨
Python Operators: The Complete Guide for Beginners
Python Operators: The Complete Guide for Beginners
Python Learning Path for Beginners 2026 🚀
Python Learning Path for Beginners 2026 🚀
Operators Explained for Beginners
Operators Explained for Beginners
7 PYTHON LIBRARIES EVERY DEVELOPER SHOULD LEARN
7 PYTHON LIBRARIES EVERY DEVELOPER SHOULD LEARN
Ultimate Python Cheat Sheet for Beginner
Ultimate Python Cheat Sheet for Beginner
🐍 Python Operators Explained | Complete Guide with Examples & Outputs
🐍 Python Operators Explained | Complete Guide with Examples & Outputs
a diagram with the words python operators on it
a diagram with the words python operators on it
Python operators infographic with cartoon elements
Python operators infographic with cartoon elements
🐍 Python Operators Cheat Sheet ✨ Learn the symbols that make Python powerful! 💻
🐍 Python Operators Cheat Sheet ✨ Learn the symbols that make Python powerful! 💻
How to read Multiple Columns from CSV File in Python? With Source Code
How to read Multiple Columns from CSV File in Python? With Source Code
Speed Up your Python Skills
Speed Up your Python Skills
All Important Python Functions for Beginners (Complete Cheat Sheet)
All Important Python Functions for Beginners (Complete Cheat Sheet)
Python Learning Path for Beginners 2026 🚀
Python Learning Path for Beginners 2026 🚀
Python operators cheat sheet infographic
Python operators cheat sheet infographic
🐍 Python – Bitwise Operators (Complete Beginner Guide)
🐍 Python – Bitwise Operators (Complete Beginner Guide)
a paper with some writing on it
a paper with some writing on it
9 Python Projects for Beginners (Build Your Portfolio Fast)
9 Python Projects for Beginners (Build Your Portfolio Fast)

Bitwise OR in form editing

In HTML forms, bitwise OR can be used to check multiple form elements (checkboxes and radio buttons) to see if they are checked. The form elements' checked state can be converted to integers for bitwise operations.

Bitwise XOR (^)

The bitwise XOR operator '^' returns 1 if the two operands have opposite binary digits at the same position, it returns 0 if they have the same binary digits. Let's use the same two integers, 13 (1101) and 3 (0011) for this operator as well. Their bitwise XOR operation results in 10 (1010).

Here's how you use it in Python:

result = 13 ^ 3
print(result)  # Outputs: 10

Using Bitwise XOR inolphra

The bitwise XOR operation is particularly useful when you need the different bits between two numbers. Suppose you have two integers where you want to know where they differ. You can use bitwise XOR to find the different positions and bits.

Bitwise XOR in data security

In data security, bitwise XOR is used for encryption and decryption thereof. A typical example is a one-time pad where the XOR of the secret message (plaintext) with a random secret key gives the encrypted message (ciphertext). Doing XOR again with the same key will give you back the original plaintext (decryption).

Learning bitwise operators can enhance your knowledge of Python and its interactions with binary representation. W3Schools provides an excellent playground to practice these operators, with examples that work and can be immediately tested in your browser. So, whether you're a student or a seasoned developer, it's a fantastic resource to learn and rehearse these powerful tools in your Python tool-kit.