Project 7: Flag and Permission Decoder

  • File: P07-flag-decoder.md
  • Main Programming Language: C
  • Alternative Programming Languages: Rust, Go, Python
  • Coolness Level: Level 3 (See REFERENCE.md)
  • Business Potential: Level 1 (See REFERENCE.md)
  • Difficulty: Level 2 (See REFERENCE.md)
  • Knowledge Area: Bitfields
  • Software or Tool: CLI
  • Main Book: “Computer Systems: A Programmer’s Perspective”

What you will build: A CLI tool that decodes a packed integer into named flags and permissions.

Why it teaches binary/hex: Flags are compact bitfields controlled by masks and shifts.

Core challenges you will face:

  • Mask extraction -> Bitwise Operations
  • Readable output -> Bits/Bytes/Nibbles
  • Edge cases -> Two’s Complement

Real World Outcome

$ flagdecode --mode 0x1ED
rwxr-xr-x
flags: USER_READ, USER_WRITE, USER_EXEC, GROUP_READ, GROUP_EXEC, OTHER_READ, OTHER_EXEC

The Core Question You Are Answering

“How do multiple boolean states fit inside one integer?”

Concepts You Must Understand First

  1. Masks and shifts
    • How to isolate a bit?
    • Book Reference: “Computer Systems: A Programmer’s Perspective” - Ch. 2
  2. Octal/hex notation
    • Why permissions are often shown in octal?
    • Book Reference: “The C Programming Language” - Ch. 2

Questions to Guide Your Design

  1. Mapping
    • How will you map bits to names?
  2. Output
    • Will you provide both symbolic and numeric formats?

Thinking Exercise

Bitfield Sketch

Draw a 9-bit field and label user/group/other permission bits.

Questions to answer:

  • Which bit corresponds to “user write”?
  • How do you clear one permission without touching others?

The Interview Questions They Will Ask

  1. “How do you extract multiple flags from one integer?”
  2. “Why is bit masking faster than multiple booleans?”
  3. “How would you represent permissions in octal?”
  4. “How do you toggle a flag bit?”
  5. “What bugs happen with signed values?”

Hints in Layers

Hint 1: Starting Point Define a table of bit positions and their names.

Hint 2: Next Level Use AND with (1«bit) to test each flag.

Hint 3: Technical Details Pseudocode:

for each flag:
  if (value AND (1<<bit)) != 0: emit name

Hint 4: Tools/Debugging Compare output to ls -l permission strings on real files.

Books That Will Help

Topic Book Chapter
Bitwise logic “Computer Systems: A Programmer’s Perspective” Ch. 2

Common Pitfalls and Debugging

Problem 1: “Flags are off by one bit”

  • Why: You mis-numbered bit positions.
  • Fix: Draw the bitfield and label each position explicitly.
  • Quick test: Use a value with only one bit set.

Definition of Done

  • Decodes at least 12 distinct flags
  • Outputs symbolic and numeric forms
  • Handles zero and all-bits-set values
  • Includes a test vector table