Skip to content
StackDecode
Bit manipulationAnimated

Bitmasking

Store a set of flags in the bits of an integer.

Time

O(1) per op

Extra space

O(1)

Use it for

Compact sets and DP states.

How it works

  1. A bitmask packs many yes/no flags into one integer: bit i is flag i.
  2. Set bit i: n | (1 << i). Clear bit i: n & ~(1 << i).
  3. Toggle bit i: n ^ (1 << i). Check bit i: (n >> i) & 1.
  4. Used for compact sets, permissions, and DP states.

Watch it run

Loading animation…