
Photo by Alex Shuper
What I Learned About Bitwise Operators and Binary Flags in Python
Eric Stober / December 4, 2025
Recently, I started working through a Python programming course. Even though I expected the early lessons to revisit concepts I already knew, I decided to stick with it because the curriculum eventually dives deep into backend development, an area I've long wanted to strengthen to complement my existing frontend skills.
To my surprise, even in the early modules I encountered a concept I had somehow never used before: bitwise operators. At first, I struggled to see why anyone would reach for binary numbers in everyday programming. What practical use could manipulating bits possibly have? Binary is for computers to worry about, or for systems programming, right?
As it turns out, there is quite a lot of practical use.
A New Way of Thinking: Binary Values as Flags
The course introduced bitwise operators using a real-world example: storing user permissions.
Instead of storing permissions as strings or multiple boolean values, you can represent them compactly using binary flags. This approach isn't just clever, it's extremely efficient, especially in systems where memory and performance matter (such as game engines, embedded systems, or backend permission models).
A single integer can represent many on/off states simultaneously, thanks to bits.
For example:
0b1000could represent create group0b0100could represent review group0b0010could represent delete group0b0001could represent edit group
You then combine or check these flags using bitwise operators.
Understanding the Bitwise AND Operator (&)
The bitwise AND operator compares each pair of bits from two numbers:
- If both bits are
1, the result is1 - Otherwise, the result is
0
This makes it perfect for checking whether a particular permission is enabled.
Example: Checking Permissions with &
# Permissions
can_create_group = 0b1000
can_review_group = 0b0100
can_delete_group = 0b0010
can_edit_group = 0b0001
# User permissions: can_review_group and can_edit_group
user_permissions = 0b0101
# Check for review permission
has_review_permission = user_permissions & can_review_group
print(has_review_permission == can_review_group) # True
# Check for create permission
has_create_permission = user_permissions & can_create_group
print(has_create_permission == can_create_group) # False
Using AND, you can instantly know which permissions a user has without storing multiple separate variables or fields.
Understanding the Bitwise OR Operator (|)
The bitwise OR operator compares each bit pair and outputs:
1if either bit is10only if both bits are0
This makes it ideal for combining permissions, turning multiple binary flags into a single value.
Basic Example
a = 0b0101 # 5
b = 0b0111 # 7
result = a | b
print(bin(result)) # 0b111
Example: Building Permission Sets with |
can_invite = 0b1000
can_kick = 0b0100
group_permissions = can_invite | can_kick
print(bin(group_permissions)) # 0b1100
In just one step, you've combined two permissions into a unified value.
Why Bitwise Operators Matter
Bitwise logic appears in more places than you might expect:
- Game development (movement flags, collision states, NPC behavior)
- Backend systems (roles, permissions, capabilities)
- Network protocols
- File system attributes
- Task scheduling and event handling
The biggest advantages are:
- Performance: binary operations are extremely fast
- Compactness: many settings can fit into one integer
- Simplicity: checking or adding a permission with a single operator
Learning bitwise operations gave me a new appreciation for how low-level concepts influence high-level design, especially in backend development, where efficient data representation can make a meaningful difference.
Final Thoughts
Before this course, bitwise operators were something I had only seen in passing and never thought I'd use. Now, I understand how powerful they can be in the right context. They offer a clean, efficient way to manage multiple states or permissions using simple binary math.
It's a great reminder that revisiting the fundamentals often unlocks new tools that can level up your work.
If you're learning Python (or programming generally), and bitwise operations come up, don't skip that part. You might find they're more useful than you expect.