Represent sets as binary numbers: how is this technique called?

216 Views Asked by At

I've been using a technique that consists in representing sets as binary numbers. This is, given a vocabulary $v = \{v_1, v_2, ..., v_n\}$ and a set $s = \{s_1, s_2, ..., s_m\}$ where $s_i \in v$, then the set $s$ can be represented as a binary number $b_s$ of length $n$ where

$$b_s[i] = \begin{cases} 1 \; \text{if} \; v[i] \in s \\ 0 \; \text{if} \; v[i] \notin s \\ \end{cases} $$

For example, if $v = \{a, b, c\}$ and $s = \{a, b\}$ then $b_s = 011$. And if $t=\{b, c\}$ then $b_t = 110$.

My question is how is this technique called. I've googled "represent a set as a binary number" and multiple variants, but nothing appears. Also, is this technique something standard?

1

There are 1 best solutions below

0
On BEST ANSWER

It's a set implemented using a bit array.

And it is a “standard” technique. The first time I encountered it was in Turbo Pascal, which used it to implement its set types.

An advantage of this representation is that set operations can easily and efficiently be implemented using bitwise operators: x & y for intersection, x | y for union, x ^ y for symmetric difference, or x & ~y for difference.

The main disadvantage is that it can't be used for sets of types with large numbers of possible values. (If int is 32 bits, then each set<int> would be $2^{32}$ bits = 512 megabytes.) For this reason, Turbo Pascal only allowed sets to contain values between 0 and 255, thus limiting the maximum size of a set object to a more practical 256 bits = 32 bytes.