Bit Masks and Flags
Set, clear, toggle bits.
Bit Masks and Flags is a free C Academy lesson on CoddyKit — lesson 3 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 C Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Bits as Flags
A single integer can store many on/off settings, one per bit. Each bit is a flag.
This is compact and fast, common in operating systems and embedded code.
#include <stdio.h>
int main(void) {
unsigned flags = 0;
printf("Initial flags = %u\n", flags);
return 0;
}Defining Masks
A mask is a value with specific bits set. Define each flag as a power of two so it occupies its own bit.
#include <stdio.h>
#define READ (1u << 0)
#define WRITE (1u << 1)
#define EXEC (1u << 2)
int main(void) {
printf("READ=%u WRITE=%u EXEC=%u\n", READ, WRITE, EXEC);
return 0;
}Setting a Bit
To turn a flag on, OR the value with the mask: flags |= MASK.
Bits already set stay set.
#include <stdio.h>
#define READ (1u << 0)
#define WRITE (1u << 1)
int main(void) {
unsigned flags = 0;
flags |= READ;
flags |= WRITE;
printf("flags = %u\n", flags);
return 0;
}Clearing a Bit
To turn a flag off, AND with the inverted mask: flags &= ~MASK.
The inverted mask has 0 in the target bit and 1 everywhere else.
#include <stdio.h>
#define READ (1u << 0)
#define WRITE (1u << 1)
int main(void) {
unsigned flags = READ | WRITE;
flags &= ~WRITE;
printf("flags = %u\n", flags);
return 0;
}Toggling a Bit
To flip a flag, XOR with the mask: flags ^= MASK.
If it was on it turns off, and vice versa.
#include <stdio.h>
#define EXEC (1u << 2)
int main(void) {
unsigned flags = 0;
flags ^= EXEC;
printf("after toggle: %u\n", flags);
flags ^= EXEC;
printf("after toggle: %u\n", flags);
return 0;
}Testing a Bit
To check whether a flag is on, AND with the mask and test the result: flags & MASK is nonzero when set.
#include <stdio.h>
#define READ (1u << 0)
#define WRITE (1u << 1)
int main(void) {
unsigned flags = READ;
if (flags & READ) printf("READ is on\n");
if (flags & WRITE) printf("WRITE is on\n");
else printf("WRITE is off\n");
return 0;
}Setting Multiple Flags
Combine masks with OR to set several flags at once.
#include <stdio.h>
#define READ (1u << 0)
#define WRITE (1u << 1)
#define EXEC (1u << 2)
int main(void) {
unsigned flags = READ | WRITE | EXEC;
printf("all set: %u\n", flags);
return 0;
}Checking Several at Once
To test that all of a group are set, AND with the combined mask and compare to the mask itself.
#include <stdio.h>
#define READ (1u << 0)
#define WRITE (1u << 1)
int main(void) {
unsigned flags = READ | WRITE;
unsigned need = READ | WRITE;
if ((flags & need) == need) printf("both present\n");
return 0;
}Printing Active Flags
Loop over your masks to report which flags are currently on.
#include <stdio.h>
#define READ (1u << 0)
#define WRITE (1u << 1)
#define EXEC (1u << 2)
int main(void) {
unsigned flags = READ | EXEC;
if (flags & READ) printf("R");
if (flags & WRITE) printf("W");
if (flags & EXEC) printf("X");
printf("\n");
return 0;
}Counting Set Bits
To know how many flags are on, count the set bits by repeatedly clearing the lowest set bit.
#include <stdio.h>
int main(void) {
unsigned v = 0b10110;
int count = 0;
while (v) {
v &= (v - 1);
count++;
}
printf("set bits = %d\n", count);
return 0;
}Enums for Flags
Using an enum keeps flag definitions organized and readable.
#include <stdio.h>
enum { OPT_A = 1, OPT_B = 2, OPT_C = 4 };
int main(void) {
unsigned opts = OPT_A | OPT_C;
printf("opts = %u\n", opts);
if (opts & OPT_C) printf("C enabled\n");
return 0;
}Quick Check
Test your knowledge of masks and flags.
Recap
You learned to manage bit flags:
- Set with
flags |= MASK. - Clear with
flags &= ~MASK. - Toggle with
flags ^= MASK. - Test with
flags & MASK. - Define masks as powers of two, often with macros or enums.
Frequently asked questions
Is the “Bit Masks and Flags” lesson free?
Yes — the full text of “Bit Masks and Flags” is free to read here on the web, and the C 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 C Academy course, upgrade to CoddyKit PRO.
What will I learn in “Bit Masks and Flags”?
Set, clear, toggle bits. You practise C 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 C Academy?
No prior experience is required. C Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Bit Masks and Flags” 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 C Academy lesson?
Yes. Every C 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
- Bitwise Operators
- Shifts
- Bit Masks and Flags
- Practical Bit Tricks