MemotivaLeetCode Patterns Flashcards: Bit Manipulation, XOR Tricks, Masks, Powers of Two

What are bit masks and how are they used to represent subsets?

LeetCode Patterns Flashcards: Bit Manipulation, XOR Tricks, Masks, Powers of Two

Аудио-карточка · 0:26

Nortren·

What are bit masks and how are they used to represent subsets?

0:26

A bit mask is an integer where each bit represents whether a corresponding element is included in a subset. For a set of n elements, an n-bit integer can represent any subset: bit at position i equals one means element i is included. Iterating from zero to two to the power of n minus one enumerates all possible subsets. To check if element i is in the mask, AND the mask with one shifted left by i. To add element i, OR the mask with one shifted left by i. To remove, AND with the complement.
neetcode.io