LearnViz CompSci
Home / Learn / Data Structures / Union of Sets with Bitsets

Advanced Topic 52 of 52 Data Structures

Union of Sets with Bitsets

Compact boolean arrays operating on many bits at once.

How it works

A bitset stores one bit per possible element, so sets over a small integer universe are represented as arrays of machine words. Union, intersection and difference become single bitwise OR, AND and AND-NOT instructions applied word by word, processing 64 elements at once. Membership tests and updates are O(1), and population count gives the set size quickly.

Step-by-step walkthrough

  1. A set over small integers is stored as bits, here positions 0 2 3 and 6.
  2. A second bitset holds another set.
  3. The union is one bitwise OR over whole machine words.
  4. Intersection is AND and difference is AND NOT.
  5. One 64-bit operation handles 64 elements at once.

Where it is used

Sieve of Eratosthenes, graph reachability and transitive closure, subset dynamic programming, Bloom-style flags, permission masks and database bitmap indexes use bitsets. Compilers use them in dataflow analysis.

Common pitfall

They suit dense sets over a bounded universe; for sparse sets with huge universes they waste memory. Elements must map to integer indexes. Shifting by the word size or more is undefined in some languages. Mind endianness and out-of-range bits in the last word, and use compressed bitmaps like Roaring for sparse data.

Where it fits in the course

Union of Sets with Bitsets is topic 52 of 52 in Data Structures, rated Advanced. It follows Rope Data Structure. It is the last topic in this area.

Practice and explore

How to study this topic

  1. Read the explanation above and restate the definition in your own words.
  2. Open the interactive lesson and answer its mini quiz.
  3. Read the common pitfall, then write a short example that avoids it.
  4. Check your understanding in the Data Structures quiz, and revisit the topic if you miss a question.

Frequently asked questions

What is Union of Sets with Bitsets?
Compact boolean arrays operating on many bits at once. It belongs to Data Structures and is an advanced-level topic.
Where do people use Union of Sets with Bitsets?
Sieve of Eratosthenes, graph reachability and transitive closure, subset dynamic programming, Bloom-style flags, permission masks and database bitmap indexes use bitsets.
What is the most common mistake with Union of Sets with Bitsets?
They suit dense sets over a bounded universe; for sparse sets with huge universes they waste memory.
What should I learn before this topic?
Start with Rope Data Structure, the topic just before it in Data Structures.
How can I practice it?
Answer the mini quiz in the interactive lesson, then take the Data Structures quiz.
Is Union of Sets with Bitsets hard to learn?
It is an Advanced topic. Expect to read the explanation more than once and to connect it with earlier topics. Working through a small example by hand helps most.
How long does it take to learn Union of Sets with Bitsets?
Most learners can read the explanation and finish the interactive lesson in under an hour. Fluency comes from applying the idea, so plan a few short practice sessions with the quiz.
Which topics are related to Union of Sets with Bitsets?
Rope Data Structure, Merkle Trees, HyperLogLog are closely connected. The full list is in the related topics section of this page.
What should I study after Union of Sets with Bitsets?
You have reached the last topic in Data Structures. Pick another area on the Learn page, or revisit this topic with the quiz.

Related topics