One bit per item to store and combine huge sets of IDs, and the Roaring trick that keeps them small.
A bitmap keeps one bit for every possible ID: 1 means "in the set", 0 means "not in the set". Combining sets (who did A and B?) becomes a few fast machine instructions. A Roaring bitmap is the same idea, but it stays small even when the set is sparse.
Imagine every user has a number: 0, 1, 2 and so on. To remember which users logged in today, you could keep a list of their numbers. Or you could keep a long row of bits, where bit number 4,001 is 1 if user 4,001 logged in. The row of bits is a bitmap.
Three operations make it useful. Set turns a bit on. Test reads a bit. And combine uses AND, OR, NOT on whole rows at once. A computer handles 64 bits in one instruction, so "users who logged in and bought something" over a billion users takes only a few milliseconds. Popcount (count the 1 bits) then gives the answer.
The cost is size. A plain bitmap needs one bit per possible ID, whether it is used or not. For one billion IDs that is 1,000,000,000 รท 8 = 125 MB. If only a thousand users logged in, a list of their IDs (4 KB) would be far smaller than 125 MB.
A Roaring bitmap splits each 32-bit ID in two. The high 16 bits choose a chunk (each chunk covers 65,536 IDs). The low 16 bits are the position inside the chunk. Each chunk picks the cheapest of three containers: an array (a sorted list of 16-bit numbers, used up to 4,096 values), a bitmap (a fixed 8 KB, used above 4,096), or runs (a list of start and length pairs). Empty chunks are not stored at all.
| Form | How it stores IDs | Good when | Bad when |
|---|---|---|---|
| List of integers | 4 bytes per ID | Very few IDs | Many IDs, and combining needs a merge |
| Plain bitmap | 1 bit per possible ID | A good share of IDs are set | Few IDs set out of a huge range |
| Compressed run-length bitmap (such as WAH) | Store runs of 0s and 1s | Long runs | Random access and updates are awkward |
| Roaring bitmap | Chunks of 65,536, each in its best container | Almost any shape of data | Very small sets (a plain list may be simpler) |
| Case | List of 32-bit ints | Plain bitmap | Roaring (roughly) |
|---|---|---|---|
| 1 billion IDs possible, 1,000 set | 4 KB | 125 MB | about 2 KB (one small array chunk per used chunk) |
| 1 billion possible, 500 million set | 2 GB | 125 MB | about 125 MB (bitmap chunks) |
| 1 billion possible, all set in ranges | 4 GB at worst | 125 MB | a few KB (run containers) |
A few hundred IDs per chunk, with one chunk empty. The universe is 4 chunks of 65,536 IDs (262,144 IDs in total).
Sizes follow the standard Roaring layout: an array container holds up to 4,096 values at 2 bytes each, a bitmap container is always 8 KB (65,536 bits), and a run container costs about 4 bytes per run. Real files add a small header per chunk, left out here. Only 16 IDs are shown in the combine demo, but a real bitmap does the same thing 64 IDs per machine word.
SETBIT dau:2026-12-04 4001 1 marks user 4001 as active. BITCOUNT dau:2026-12-04 counts active users. BITOP AND both dau:2026-12-03 dau:2026-12-04 finds users active on both days.| Term | Meaning in easy words |
|---|---|
| Bitmap | A row of bits where bit i says whether ID i is in the set |
| Bitset | Another name for a bitmap |
| Popcount | Counting how many bits are set to 1 |
| Bitmask | A small bitmap used as a set of flags |
| Bitmap index | A database index that keeps one bitmap per distinct value |
| Cardinality | How many different values a column has, or how many IDs are in a set |
| Sparse | Few IDs set out of a huge range |
| Dense | A large share of the range is set |
| Roaring bitmap | A compressed bitmap that splits IDs into chunks of 65,536 |
| Container | How one Roaring chunk is stored: array, bitmap or run |
| Run-length encoding | Storing a stretch of equal values as a start and a length |
| Choice | We gain | We lose |
|---|---|---|
| Plain bitmap | Simplest and fastest | Wastes memory on sparse sets |
| List of ints | Small for tiny sets | Slow to combine, large when dense |
| Roaring bitmap | Small and fast in most shapes | More code, a library to depend on |
| Bitmap index in a database | Fast multi-filter queries | Costly to update on write-heavy tables |
| Exact bitmap counts | Exact answers | More memory than HyperLogLog |