Skip to content
Guest
Learn C · Course Syllabus Show lessons
Systems Programming · 8 min read · Intermediate

Enums, Unions & Bitwise Flags

Represent finite states cleanly with enums and pack multiple boolean options into one integer using bitwise masks.

What you'll learn

  • Define readable named constants with enum
  • Combine and test bit flags using |, &, and ~
  • Understand how unions share memory across alternative fields

Systems code in C relies heavily on enums for state machines and bitwise flags (1 << 0, 1 << 1, 1 << 2) to pack permissions or configuration options into a single byte or integer.

  • Set flag: perms |= FLAG_WRITE;
  • Check flag: if (perms & FLAG_WRITE)
  • Clear flag: perms &= ~FLAG_WRITE;

Fill-in-the-Blank Exercise

Combine & Test Bit Flags

Combine READ and WRITE flags with |, and test WRITE with &.

unsigned int perms = FLAG_READ  FLAG_WRITE;
if (perms  FLAG_WRITE) {
  printf("Writable\n");
}

Live Code Lab

Cbitflags.c
Expected OutputClick “Run Live” to compile with GCC 13 (C11)
Can write? yes
Can exec?  no

Check Your Understanding

Score: 0 / 1

1. Which bitwise expression clears FLAG_WRITE from perms without affecting other flags?

Study Workspace

Finished this lesson?

Your progress is saved locally in your browser, no account needed.

Search courses, lessons, and references

Type to search across all courses, lessons, and quick references.

Press Esc to close Powered by Pagefind