Set, Clear & Toggle a Bit
Edit a single bit with masks.
Set, Clear & Toggle a Bit is a free Competitive Programming Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Competitive Programming Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Edit One Bit at a Time
Sometimes you need to change a single bit without touching the others. The trick is building a mask that targets exactly the bit you want. 🎯
Build a Single-Bit Mask
Shift 1 to the position you care about and you get a mask with one bit on. Position 0 is the rightmost bit.
mask = 1 << 3 # 0b1000, targets bit 3Set a Bit with OR
To turn a bit on, OR the number with the mask. OR forces that position to 1 and leaves every other bit alone. We call this setting a bit.
n = 0b0001
n |= (1 << 2) # 0b0101Setting Is Idempotent
If the bit is already 1, setting it again changes nothing. OR with a mask is safe to repeat, which makes it reliable inside loops. 👍
The Inverted Mask
To clear a bit you need a mask with that position OFF and all others ON. The NOT operator (~) flips every bit of your simple mask.
clear = ~(1 << 2) # ...11111011Clear a Bit with AND
AND the number with the inverted mask to force that bit to 0 while keeping the rest. This is how you clear a single bit.
n = 0b0111
n &= ~(1 << 1) # 0b0101Toggle a Bit with XOR
XOR with a single-bit mask flips that position: 0 becomes 1 and 1 becomes 0. This toggle is the cleanest one-liner of the three.
n = 0b0101
n ^= (1 << 0) # 0b0100Check If a Bit Is On
AND the number with a single-bit mask. If the result is nonzero, that bit is set; if it is zero, the bit is off.
n = 0b0100
on = (n & (1 << 2)) != 0 # TrueRead the Bit as 0 or 1
Shift the number right to bring your target bit to the bottom, then AND with 1. You get exactly the bit value, 0 or 1.
n = 0b1010
bit = (n >> 1) & 1 # 1Why Bit Edits Matter
Setting, clearing, and toggling let you store many on or off flags inside one integer. That is the heart of compact bitmask state in contests.
Mind the Off-by-One
Bit positions start at 0, so bit k lives at value 1 shifted left by k. Mixing up the index is the most common beginner bug here.
Quick Check
Pick the right operation for clearing a bit.
Recap: Set, Clear, Toggle
OR sets, AND with NOT clears, and XOR toggles a single bit. Master these three masks and you control any bit you like. 🎉
Frequently asked questions
Is the “Set, Clear & Toggle a Bit” lesson free?
Yes — the full text of “Set, Clear & Toggle a Bit” is free to read here on the web, and the Competitive Programming Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Competitive Programming Academy course, upgrade to CoddyKit PRO.
What will I learn in “Set, Clear & Toggle a Bit”?
Edit a single bit with masks. You practise Competitive Programming Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Competitive Programming Academy?
No prior experience is required. Competitive Programming Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Set, Clear & Toggle a Bit” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Competitive Programming Academy lesson?
Yes. Every Competitive Programming Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- AND, OR, XOR & Shifts
- Set, Clear & Toggle a Bit
- Count Bits and Lowest Set Bit
- Bitmasks as Tiny Sets