diff --git a/luku10.tex b/luku10.tex index d56302b..0b81748 100644 --- a/luku10.tex +++ b/luku10.tex @@ -278,26 +278,26 @@ Each subset of a set $\{0,1,2,\ldots,n-1\}$ corresponds to a $n$ bit number where the one bits indicate which elements are included in the subset. -For example, the bit representation for $\{1,3,4,8\}$ +For example, the bit representation of $\{1,3,4,8\}$ is 100011010 that equals $2^8+2^4+2^3+2^1=282$. -The bit representation of a set uses little memory -because only one bit is needed for the information -whether an element belongs to the set. -In addition, we can efficiently manipulate sets -that are stored as bits. +The benefit in using a bit representation is +that the information whether an element belongs +to the set requires only one bit of memory. +In addition, we can implement set operations +efficiently as bit operations. \subsubsection{Set operations} -In the following code, the variable $x$ +In the following code, $x$ contains a subset of $\{0,1,2,\ldots,31\}$. -The code adds elements 1, 3, 4 and 8 -to the set and then prints the elements in the set. +The code adds the elements 1, 3, 4 and 8 +to the set and then prints the elements. \begin{lstlisting} // x is an empty set int x = 0; -// add numbers 1, 3, 4 and 8 to the set +// add elements 1, 3, 4 and 8 to the set x |= (1<<1); x |= (1<<3); x |= (1<<4); @@ -309,24 +309,13 @@ for (int i = 0; i < 32; i++) { cout << "\n"; \end{lstlisting} -The output of the code is as follows: -\begin{lstlisting} -1 3 4 8 -\end{lstlisting} - -Using the bit representation of a set, -we can efficiently implement set operations -using bit operations: +Set operations can be implemented as follows: \begin{itemize} \item $a$ \& $b$ is the intersection $a \cap b$ of $a$ and $b$ -(this contains the elements that are in both the sets) \item $a$ | $b$ is the union $a \cup b$ of $a$ and $b$ -(this contains the elements that are at least -in one of the sets) +\item \textasciitilde$a$ is the complement $\bar a$ of $a$ \item $a$ \& (\textasciitilde$b$) is the difference $a \setminus b$ of $a$ and $b$ -(this contains the elements that are in $a$ -but not in $b$) \end{itemize} The following code constructs the union @@ -346,14 +335,9 @@ for (int i = 0; i < 32; i++) { cout << "\n"; \end{lstlisting} -The output of the code is as follows: -\begin{lstlisting} -1 3 4 6 8 9 -\end{lstlisting} - \subsubsection{Iterating through subsets} -The following code iterates through +The following code goes through the subsets of $\{0,1,\ldots,n-1\}$: \begin{lstlisting} @@ -362,7 +346,7 @@ for (int b = 0; b < (1<