Python Enums & Bitwise Manipulation (Flags & Masks)
An enum defines named integer constants for readable state machines. Bitwise operators (&, |, ^, ~, <<, >>) allow packing up to 32 independent boolean feature flags into a single 4-byte integer using bitmasks.
"Bitmasks are like a row of 32 light switches on an electrical circuit panel: a single integer code tells you exactly which switches are ON and which are OFF."
Deep Dive: How It Works
Bitwise Shift (<<): 1 << n sets a 1 at bit position n (values 1, 2, 4, 8, 16...).
Bitwise OR (|): Combines multiple flags together into a single composite state mask.
Bitwise AND (&): Tests if a specific bit flag is active within a mask.
Syntax Blueprint
#define FLAG_A (1 << 0)
#define FLAG_B (1 << 1)
int mask = FLAG_A | FLAG_B;
if (mask & FLAG_A) { ... }Define binary bit flags with shifts and check membership using bitwise AND.
Core Rules to Remember



Common Beginner Traps & How to Fix Them
Confusing logical operators (&&, ||) with bitwise operators (&, |).Why it happens: && is a boolean test with short-circuiting; & performs bit-level arithmetic across all 32 bits.
How to fix: Use & and | for bitmasks and flags; use && and || for logical condition tests.
Live Interactive Example
Hit Run Code to see it liveYour Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Verify VIP Bit Flag
Add a check with "if (userFlags & FLAG_VIP)" that prints "User is VIP\n".
Finished reading and practicing?
Mark this lesson as completed to update your course progress.