Ad space (banner)
🐍Python Lessons
Lesson 55 / 69

Bitwise Operators (AND, OR, XOR, shifts)

This lesson covers the basics of bitwise operators in Python, so you can understand this low-level way of treating a number as binary. It's written for anyone searching "Python bitwise AND OR".

Bitwise operators treat a number as binary and compute bit by bit. They include & (AND), | (OR), ^ (XOR), and << (left shift), and they're commonly used for flag management and other low-level tasks. This requires a different mindset than ordinary arithmetic — comparing and computing digit by digit, in binary.

The sample code tries all four operations — AND, OR, XOR, and left shift — on two numbers, 12 (binary 1100) and 10 (binary 1010). & is 1 only when both bits are 1, | is 1 when either bit is 1 — the final number is built up from these per-digit results.

You'll rarely need it in everyday app development, but it's useful knowledge close to the machine, helping you understand the basics of image processing or encryption. Packing several on/off flags into a single number — a technique known as "bit flags" — is a classic application of bitwise operators.

It's also used for memory-efficient techniques, like managing several flags together in a single number. Bitwise operations come up whenever you venture into lower-level territory.

Python
OUTPUT

💡 The Python engine may take a few seconds to load the first time you run code.

Ad space (banner)
Ad space (in-article)