
Bitwise Flags in Practice: Permissions, Toggles, Masks
A bitmask stores several yes/no flags in one integer. OR sets selected bits, AND checks them, and XOR toggles them. The examples below use a small permission field so you can follow each operation in binary.
A tiny permission scheme
Imagine a file permission byte with four flags (we only use the low nibble):
| Flag | Bit | Mask | Binary |
|---|---|---|---|
| READ | 0 | 1 | 00000001 |
| WRITE | 1 | 2 | 00000010 |
| EXEC | 2 | 4 | 00000100 |
| ADMIN | 3 | 8 | 00001000 |
A user with READ+WRITE gets mask 3 (00000011). ADMIN alone is 8. Combining flags means OR-ing the masks.
OR: turn flags on
Start with empty permissions (0). Grant READ and EXEC:
perms = 0
perms = perms | READ | EXEC
0 | 1 | 4 = 5 (00000101)
OR never clears a bit that was already 1. It only sets bits where either side has a 1.
AND: test a flag
Check whether WRITE is present in 5:
5 & WRITE → 00000101 & 00000010 = 00000000
Result is 0 → WRITE is off
5 & READ → 00000101 & 00000001 = 00000001
Non-zero → READ is on
In code you usually write if (perms & WRITE). Truthiness of the masked result is enough.
AND with NOT: clear a flag
To remove EXEC from 5 without touching other bits:
perms = 5 & ~EXEC
~EXEC in 8-bit view of the low bits: clear bit 2
00000101 & 11111011 = 00000001 (only READ left)
Watch language width: in JavaScript, ~ operates on 32-bit signed ints. Mask with 0xff when you care about a single byte.
XOR: flip or toggle
XOR with a mask flips exactly those bits. Toggle ADMIN on a value of 1 (READ only):
00000001 XOR 00001000 = 00001001 (9)
XOR again with ADMIN → back to 00000001
A configuration field can hold several independent toggles. XOR changes only the bits selected by the mask. Updating a shared value still requires the appropriate synchronization; a bitwise expression alone does not make a read-modify-write operation atomic.
Feature toggle word example
Suppose a product ships with a 16-bit feature field. Low bits:
- bit 0: dark mode UI
- bit 1: beta dashboard
- bit 2: verbose logging
Server sends 0b00000101 (5). Client checks:
flags & 1 → dark mode on
flags & 2 → beta off
flags & 4 → verbose on
Practice set (do these by hand)
- Start at 0. Set READ|WRITE|ADMIN. What decimal do you get?
- From that value, clear WRITE. What remains?
- Does
13 & 4mean EXEC is on? - What is
0b1100 XOR 0b0101?
Answers
- 1|2|8 = 11 (
00001011) - 11 & ~2 → 9 (
00001001) - 13 is
1101; bit 2 is set → yes, EXEC on 1001= 9
Visualize bits while you practice
The bitwise playground on the home converter suite shows AND, OR, XOR, and NOT on two bytes with live bit chips. Use it to check the practice answers.
For more operator context in real languages, read binary operations in programming. This article stays on flags; that one covers shifts and masks in broader code.
When flags beat booleans
- Wire protocols and binary file formats with fixed-size option fields
- Kernels and drivers where every byte in a struct matters
- Gamepad / HID reports packing many buttons into one report byte
- Database columns storing a compact set of attributes
In application UI code, separate booleans are often clearer. Reach for bitmasks when the storage format or API already speaks in flags.
FAQ
Why powers of two for each flag?
Each flag needs its own bit. Powers of two are exactly one bit set: 1, 2, 4, 8, 16, …. OR-ing them never collapses two flags into one bit.
Is | the same as + for flags?
Only when the flags do not overlap. 1+2+4 = 7 and 1|2|4 = 7. But 1+1 = 2 while 1|1 = 1. Always OR when combining masks so duplicates stay safe.
How do I inspect a flag byte from a file?
Dump the file with the hex viewer, note the byte, then test masks by hand or in the playground on the home page.
Do signed integers change flag tests?
Prefer unsigned types for pure bitmasks. Sign bits and arithmetic right shifts add confusion you do not need for permission fields. See two's complement material if you must mix signed values with bit ops.
Open the playground
Set A to 13 and B to 4 on the bitwise playground, try AND/OR/XOR, and match the practice answers above. Then skim the programming guide for shift patterns you will meet next.
Check the examples
Technical reference
Related Articles
Binary Operations in Programming: A Practical Guide
Use bitwise operators in JavaScript examples. Bitwise operators, masks, flags, and performance optimization techniques.

Octal Explained: Convert Octal to Binary and Decimal
Octal groups bits by threes. Learn why Unix permissions use it and how to convert between octal, binary, and decimal cleanly.

Signed Binary and Two's Complement (Negative Numbers)
How computers store negative integers: invert the bits, add one, and check the range of an 8-bit signed value.
