Coding/Learn/c/c enums and bitwise operators
C Foundational Course
01.Introduction, Compilation Pipeline & Syntax
02.Variables, Primitives & Explicit Type Casting
03.Operators, Precedence & Booleans (<stdbool.h>)
04.Control Flow: If-Else, Ternary & Switch
05.Loops (For, While, Do-While) & Jump Statements
06.Contiguous Arrays & Multi-Dimensional Matrices
07.Null-Terminated Strings & <string.h>
08.User Input, scanf & fgets Buffer Safety
09.Pointers, Memory Addresses & Dereferencing
10.Functions, Prototypes & Pass-by-Pointer
11.11. Recursion & High-Speed Function Pointers
12.File Streams: Create, Write & Read (fopen/fclose)
13.Structures, Unions & typedef Aliasing
14.Memory Word Alignment & Struct Padding
15.Enums & Bitwise Manipulation (Flags & Masks)
16.Dynamic Memory: malloc, calloc, realloc & free
17.Defensive Error Handling, errno & Assertions
18.Preprocessor Macros & Storage Classes
19.Fixed-Width Types (stdint.h), Math & Time (<time.h>)
20.Capstone: High-Performance In-Memory Key-Value Engine
Course Certificate0%

Complete all lessons to earn

LearnEnums & Bitwise Manipulation (Flags & Masks)
15 / 20 0%
Lesson 15 of 20
6 mins read

Enums & Bitwise Manipulation (Flags & Masks)

PreviousNext Lesson
In Plain English

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.

The Mental Model

"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.

PointCore Rules to Remember

Point star
Compact Storage: Packs 32 discrete booleans into a single 32-bit integer register.
Point star
Bitwise AND Test: mask & FLAG returns non-zero if the flag bit is set to 1.
Point star
Bitwise XOR Toggle: mask ^= FLAG flips the bit between 0 and 1.

Common Beginner Traps & How to Fix Them

Mistake: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.

Pro Tip: Use uppercase enum identifiers (LOG_DEBUG, LOG_INFO) for clear semantic state management.

Live Interactive Example

Hit Run Code to see it live
Bitmask Permission Flags
C11 / GCC 9.2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Output Console
Click "Run Code" to view the rendered output.
How it works: FLAG_ACTIVE (1) | FLAG_VIP (4) = 5. Bitwise AND checks if bit 0 is set.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Verify VIP Bit Flag

  • PointAdd a check with "if (userFlags & FLAG_VIP)" that prints "User is VIP\n".
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates