CS Fundamentals intermediate 12 min

Debug a Bitmask Permission Check

Use fixed-width bitwise operations to explain why one permission bit is missing.

Scenario

A user should have read and export access, but a support screenshot shows the export button disabled. Permissions are stored as a bitmask, and the UI gates the button with mask & EXPORT_FLAG. Someone claims the mask is right; someone else claims the flag constant is wrong. Doing binary AND in your head under pressure is how this bug shipped in the first place — evaluate it explicitly.

Broken input

stored mask: 0b0101
export flag: 0b1000
check: mask & flag

Goal

Use Bitwise Operations Calculator to prove whether the stored bitmask includes the permission bit the UI is checking. A bitwise AND that evaluates to zero means the permission is genuinely absent — the UI is behaving correctly and the data is wrong.

Tool sequence

  1. Open Bitwise Operations Calculator.
  2. Enter 0101 and 1000 as the two operands and select AND, keeping the bit width at the size the application uses (here 4 bits is enough).
  3. Read the result row — the calculator shows the operation bit by bit, so you can see which positions overlap.
  4. 0101 & 1000 produces 0000: no overlap, so the export bit is simply not set in the stored mask.
  5. Cross-check the flag constants in Binary / Hex / Decimal Converter — a wrong constant like 8 where the schema says export is bit 2 (4) produces exactly this symptom.

Checkpoint output

  0101   (stored mask: read + execute bits)
& 1000   (export flag, bit 3)
------
  0000   → export permission NOT present

The AND result is zero, so the stored mask never contained the export bit. The fix is in whatever writes the mask, not in the UI check.

Common mistakes

  • Comparing decimal-looking values like 5 and 8 without converting them to binary at the same width first.
  • Using OR when you meant to test membership — mask | flag is always truthy, so the check silently passes for everyone.
  • Ignoring the signed/unsigned interpretation when the high bit is involved at full register width.
  • Fixing the constant in one place while the same magic number lives in a migration or a second service.

Related tools and lessons