Bitwise Tricks in C
From the C cheat sheet · Bit Manipulation · verified Jul 2026
Bitwise Tricks
Masks, flags, and shifts.
c
// Quick Reference
x & mask // test bits
x | flag // set bits
x & ~flag // clear bits
x ^ flag // toggle bits
x << n // shift left💡 1u << n makes a mask for bit n; use it to set, clear, toggle, or test that bit.
⚡ Combine flags with |, test with &, remove with & ~flag.
📌 Prefer unsigned types for bit work - right-shifting signed negatives is implementation-defined.
🟢 x << 1 and x >> 1 are fast multiply/divide by 2 (for non-negative values).
bitwiseflags