\section{Hamming Codes} Say we have a 16-bit message, for example \texttt{1011 0101 1101 1001}. \par We will number its bits in binary, from left to right: \begin{center} \begin{tikzpicture} \node[anchor=west] at (-1.75, 0) {Bit}; \node[anchor=west] at (-1.75, -0.5) {Index}; \node at (0, 0) {\texttt{1}}; \node at (1, 0) {\texttt{0}}; \node at (2, 0) {\texttt{1}}; \node at (3, 0) {\texttt{1}}; \node at (4, 0) {\texttt{0}}; \node at (5, 0) {\texttt{1}}; \node at (6, 0) {\texttt{0}}; \node at (7, 0) {\texttt{1}}; \draw (-1.75, 0.25) -- (6.9, 0.25); \draw (-1.75, -0.25) -- (6.9, -0.25); \draw (-1.75, -0.75) -- (6.9, -0.75); \foreach \x in {-1.75,-0.5,0.5,...,6.5} { \draw (\x, 0.25) -- (\x, -0.75); } \node[color=gray] at (0, -0.5) {\texttt{0000}}; \node[color=gray] at (1, -0.5) {\texttt{0001}}; \node[color=gray] at (2, -0.5) {\texttt{0010}}; \node[color=gray] at (3, -0.5) {\texttt{0011}}; \node[color=gray] at (4, -0.5) {\texttt{0100}}; \node[color=gray] at (5, -0.5) {\texttt{0101}}; \node[color=gray] at (6, -0.5) {\texttt{0110}}; \node[color=gray] at (7, -0.5) {\texttt{0111}}; \draw[fill = white, draw = none] (6.9, 0.25) -- (7.1, 0) -- (6.9, -0.25) -- (7.1, -0.5) -- (6.9, -0.75) -- (7.5, -0.75) -- (7.5, 0.25) ; \draw (6.9, 0.25) -- (7.1, 0) -- (6.9, -0.25) -- (7.1, -0.5) -- (6.9, -0.75) ; \node[anchor=west,color=gray] at (7.2, -0.25) { and so on...}; \end{tikzpicture} \end{center} \problem{} In this 16-bit message, how many message bits have an index with a one as the last digit? \par (i.e, an index that looks like \texttt{***1}) \vspace{2cm} \problem{} Say we number the bits in a 32-bit message as above. \par How many message bits have an index with a one as the $n^\text{th}$ digit? \par \vspace{2cm} We now want a way to detect errors in our 16-bit message. To do this, we'll replace a few data bits with pairity bits. This will reduce the amount of information we can send, but will also improve our error-detection capabilities. \vspace{1mm} Let's arrange our message in a grid. We'll make the first bit (currently empty, marked \texttt{X}) a pairity bit. Its value will depend on the content of the message: if our message has an even number of ones, it will be zero; if our message has an odd number of ones, it will be one. \begin{center} \begin{tikzpicture}[scale = 1.25] \node at (0.0, 0) {\texttt{X}}; \node at (0.5, 0) {\texttt{0}}; \node at (1.0, 0) {\texttt{1}}; \node at (1.5, 0) {\texttt{1}}; \node at (0.0, -0.5) {\texttt{0}}; \node at (0.5, -0.5) {\texttt{1}}; \node at (1.0, -0.5) {\texttt{0}}; \node at (1.5, -0.5) {\texttt{1}}; \node at (0.0, -1) {\texttt{1}}; \node at (0.5, -1) {\texttt{1}}; \node at (1.0, -1) {\texttt{0}}; \node at (1.5, -1) {\texttt{1}}; \node at (0.0, -1.5) {\texttt{1}}; \node at (0.5, -1.5) {\texttt{0}}; \node at (1.0, -1.5) {\texttt{0}}; \node at (1.5, -1.5) {\texttt{1}}; \draw (-0.25, 0.25) -- (1.75, 0.25); \draw (-0.25, -0.25) -- (1.75, -0.25); \draw (-0.25, -0.75) -- (1.75, -0.75); \draw (-0.25, -1.25) -- (1.75, -1.25); \draw (-0.25, -1.75) -- (1.75, -1.75); \draw (-0.25, 0.25) -- (-0.25, -1.75); \draw (0.25, 0.25) -- (0.25, -1.75); \draw (0.75, 0.25) -- (0.75, -1.75); \draw (1.25, 0.25) -- (1.25, -1.75); \draw (1.75, 0.25) -- (1.75, -1.75); \draw (-0.2,-0.2) -- (0.2, -0.2) -- (0.2, 0.2) -- (-0.2, 0.2) -- (-0.2,-0.2); \end{tikzpicture} \end{center} \problem{} What is the value of the pairity bit in the message above? \vfill \problem{} Can this coding scheme detect a transposition error? \par Can this coding scheme detect two single-bit errors? \par Can this coding scheme correct a single-bit error? \vfill \pagebreak We'll now add four more pairity bits, in positions \texttt{0001}, \texttt{0010}, \texttt{0100}, and \texttt{1000}: \begin{center} \begin{tikzpicture}[scale = 1.25] \node at (0.0, 0) {\texttt{X}}; \node at (0.5, 0) {\texttt{X}}; \node at (1.0, 0) {\texttt{X}}; \node at (1.5, 0) {\texttt{1}}; \node at (0.0, -0.5) {\texttt{X}}; \node at (0.5, -0.5) {\texttt{1}}; \node at (1.0, -0.5) {\texttt{0}}; \node at (1.5, -0.5) {\texttt{1}}; \node at (0.0, -1) {\texttt{X}}; \node at (0.5, -1) {\texttt{1}}; \node at (1.0, -1) {\texttt{0}}; \node at (1.5, -1) {\texttt{1}}; \node at (0.0, -1.5) {\texttt{1}}; \node at (0.5, -1.5) {\texttt{0}}; \node at (1.0, -1.5) {\texttt{0}}; \node at (1.5, -1.5) {\texttt{1}}; \draw (-0.25, 0.25) -- (1.75, 0.25); \draw (-0.25, -0.25) -- (1.75, -0.25); \draw (-0.25, -0.75) -- (1.75, -0.75); \draw (-0.25, -1.25) -- (1.75, -1.25); \draw (-0.25, -1.75) -- (1.75, -1.75); \draw (-0.25, 0.25) -- (-0.25, -1.75); \draw (0.25, 0.25) -- (0.25, -1.75); \draw (0.75, 0.25) -- (0.75, -1.75); \draw (1.25, 0.25) -- (1.25, -1.75); \draw (1.75, 0.25) -- (1.75, -1.75); \draw (0 - 0.2, 0 - 0.2) -- (0 + 0.2, 0 - 0.2) -- (0 + 0.2, 0 + 0.2) -- (0 - 0.2, 0 + 0.2) -- (0 - 0.2, 0 - 0.2); \draw (0.5 - 0.2, 0 - 0.2) -- (0.5 + 0.2, 0 - 0.2) -- (0.5 + 0.2, 0 + 0.2) -- (0.5 - 0.2, 0 + 0.2) -- (0.5 - 0.2, 0 - 0.2); \draw (1 - 0.2, 0 - 0.2) -- (1 + 0.2, 0 - 0.2) -- (1 + 0.2, 0 + 0.2) -- (1 - 0.2, 0 + 0.2) -- (1 - 0.2, 0 - 0.2); \draw (0 - 0.2, -0.5 - 0.2) -- (0 + 0.2, -0.5 - 0.2) -- (0 + 0.2, -0.5 + 0.2) -- (0 - 0.2, -0.5 + 0.2) -- (0 - 0.2, -0.5 - 0.2); \draw (0 - 0.2, -1 - 0.2) -- (0 + 0.2, -1 - 0.2) -- (0 + 0.2, -1 + 0.2) -- (0 - 0.2, -1 + 0.2) -- (0 - 0.2, -1 - 0.2); \end{tikzpicture} \end{center} Bit \texttt{0001} will count the pairity of all bits with a one in the first digit of their index. \par Bit \texttt{0010} will count the pairity of all bits with a one in the second digit of their index. \par Bits \texttt{0100} and \texttt{1000} work in the same way. \problem{} Compute all pairity bits in the message above. \vfill \problem{} Each of the following messages has either 0, 1, or two errors. \par Find the errors and correct them if possible. \begin{center} \hfill \begin{tikzpicture}[scale = 1.25] \node at (0.0, 0) {\texttt{0}}; \node at (0.5, 0) {\texttt{1}}; \node at (1.0, 0) {\texttt{1}}; \node at (1.5, 0) {\texttt{1}}; \node at (0.0, -0.5) {\texttt{0}}; \node at (0.5, -0.5) {\texttt{1}}; \node at (1.0, -0.5) {\texttt{1}}; \node at (1.5, -0.5) {\texttt{1}}; \node at (0.0, -1) {\texttt{0}}; \node at (0.5, -1) {\texttt{0}}; \node at (1.0, -1) {\texttt{1}}; \node at (1.5, -1) {\texttt{1}}; \node at (0.0, -1.5) {\texttt{1}}; \node at (0.5, -1.5) {\texttt{1}}; \node at (1.0, -1.5) {\texttt{1}}; \node at (1.5, -1.5) {\texttt{0}}; \draw (-0.25, 0.25) -- (1.75, 0.25); \draw (-0.25, -0.25) -- (1.75, -0.25); \draw (-0.25, -0.75) -- (1.75, -0.75); \draw (-0.25, -1.25) -- (1.75, -1.25); \draw (-0.25, -1.75) -- (1.75, -1.75); \draw (-0.25, 0.25) -- (-0.25, -1.75); \draw (0.25, 0.25) -- (0.25, -1.75); \draw (0.75, 0.25) -- (0.75, -1.75); \draw (1.25, 0.25) -- (1.25, -1.75); \draw (1.75, 0.25) -- (1.75, -1.75); \draw (0 - 0.2, 0 - 0.2) -- (0 + 0.2, 0 - 0.2) -- (0 + 0.2, 0 + 0.2) -- (0 - 0.2, 0 + 0.2) -- (0 - 0.2, 0 - 0.2); \draw (0.5 - 0.2, 0 - 0.2) -- (0.5 + 0.2, 0 - 0.2) -- (0.5 + 0.2, 0 + 0.2) -- (0.5 - 0.2, 0 + 0.2) -- (0.5 - 0.2, 0 - 0.2); \draw (1 - 0.2, 0 - 0.2) -- (1 + 0.2, 0 - 0.2) -- (1 + 0.2, 0 + 0.2) -- (1 - 0.2, 0 + 0.2) -- (1 - 0.2, 0 - 0.2); \draw (0 - 0.2, -0.5 - 0.2) -- (0 + 0.2, -0.5 - 0.2) -- (0 + 0.2, -0.5 + 0.2) -- (0 - 0.2, -0.5 + 0.2) -- (0 - 0.2, -0.5 - 0.2); \draw (0 - 0.2, -1 - 0.2) -- (0 + 0.2, -1 - 0.2) -- (0 + 0.2, -1 + 0.2) -- (0 - 0.2, -1 + 0.2) -- (0 - 0.2, -1 - 0.2); \end{tikzpicture} \hfill \begin{tikzpicture}[scale = 1.25] \node at (0.0, 0) {\texttt{1}}; \node at (0.5, 0) {\texttt{1}}; \node at (1.0, 0) {\texttt{0}}; \node at (1.5, 0) {\texttt{1}}; \node at (0.0, -0.5) {\texttt{1}}; \node at (0.5, -0.5) {\texttt{0}}; \node at (1.0, -0.5) {\texttt{1}}; \node at (1.5, -0.5) {\texttt{0}}; \node at (0.0, -1) {\texttt{0}}; \node at (0.5, -1) {\texttt{1}}; \node at (1.0, -1) {\texttt{1}}; \node at (1.5, -1) {\texttt{0}}; \node at (0.0, -1.5) {\texttt{1}}; \node at (0.5, -1.5) {\texttt{1}}; \node at (1.0, -1.5) {\texttt{0}}; \node at (1.5, -1.5) {\texttt{1}}; \draw (-0.25, 0.25) -- (1.75, 0.25); \draw (-0.25, -0.25) -- (1.75, -0.25); \draw (-0.25, -0.75) -- (1.75, -0.75); \draw (-0.25, -1.25) -- (1.75, -1.25); \draw (-0.25, -1.75) -- (1.75, -1.75); \draw (-0.25, 0.25) -- (-0.25, -1.75); \draw (0.25, 0.25) -- (0.25, -1.75); \draw (0.75, 0.25) -- (0.75, -1.75); \draw (1.25, 0.25) -- (1.25, -1.75); \draw (1.75, 0.25) -- (1.75, -1.75); \draw (0 - 0.2, 0 - 0.2) -- (0 + 0.2, 0 - 0.2) -- (0 + 0.2, 0 + 0.2) -- (0 - 0.2, 0 + 0.2) -- (0 - 0.2, 0 - 0.2); \draw (0.5 - 0.2, 0 - 0.2) -- (0.5 + 0.2, 0 - 0.2) -- (0.5 + 0.2, 0 + 0.2) -- (0.5 - 0.2, 0 + 0.2) -- (0.5 - 0.2, 0 - 0.2); \draw (1 - 0.2, 0 - 0.2) -- (1 + 0.2, 0 - 0.2) -- (1 + 0.2, 0 + 0.2) -- (1 - 0.2, 0 + 0.2) -- (1 - 0.2, 0 - 0.2); \draw (0 - 0.2, -0.5 - 0.2) -- (0 + 0.2, -0.5 - 0.2) -- (0 + 0.2, -0.5 + 0.2) -- (0 - 0.2, -0.5 + 0.2) -- (0 - 0.2, -0.5 - 0.2); \draw (0 - 0.2, -1 - 0.2) -- (0 + 0.2, -1 - 0.2) -- (0 + 0.2, -1 + 0.2) -- (0 - 0.2, -1 + 0.2) -- (0 - 0.2, -1 - 0.2); \end{tikzpicture} \hfill \begin{tikzpicture}[scale = 1.25] \node at (0.0, 0) {\texttt{0}}; \node at (0.5, 0) {\texttt{1}}; \node at (1.0, 0) {\texttt{1}}; \node at (1.5, 0) {\texttt{1}}; \node at (0.0, -0.5) {\texttt{1}}; \node at (0.5, -0.5) {\texttt{0}}; \node at (1.0, -0.5) {\texttt{1}}; \node at (1.5, -0.5) {\texttt{1}}; \node at (0.0, -1) {\texttt{1}}; \node at (0.5, -1) {\texttt{0}}; \node at (1.0, -1) {\texttt{1}}; \node at (1.5, -1) {\texttt{1}}; \node at (0.0, -1.5) {\texttt{1}}; \node at (0.5, -1.5) {\texttt{0}}; \node at (1.0, -1.5) {\texttt{0}}; \node at (1.5, -1.5) {\texttt{0}}; \draw (-0.25, 0.25) -- (1.75, 0.25); \draw (-0.25, -0.25) -- (1.75, -0.25); \draw (-0.25, -0.75) -- (1.75, -0.75); \draw (-0.25, -1.25) -- (1.75, -1.25); \draw (-0.25, -1.75) -- (1.75, -1.75); \draw (-0.25, 0.25) -- (-0.25, -1.75); \draw (0.25, 0.25) -- (0.25, -1.75); \draw (0.75, 0.25) -- (0.75, -1.75); \draw (1.25, 0.25) -- (1.25, -1.75); \draw (1.75, 0.25) -- (1.75, -1.75); \draw (0 - 0.2, 0 - 0.2) -- (0 + 0.2, 0 - 0.2) -- (0 + 0.2, 0 + 0.2) -- (0 - 0.2, 0 + 0.2) -- (0 - 0.2, 0 - 0.2); \draw (0.5 - 0.2, 0 - 0.2) -- (0.5 + 0.2, 0 - 0.2) -- (0.5 + 0.2, 0 + 0.2) -- (0.5 - 0.2, 0 + 0.2) -- (0.5 - 0.2, 0 - 0.2); \draw (1 - 0.2, 0 - 0.2) -- (1 + 0.2, 0 - 0.2) -- (1 + 0.2, 0 + 0.2) -- (1 - 0.2, 0 + 0.2) -- (1 - 0.2, 0 - 0.2); \draw (0 - 0.2, -0.5 - 0.2) -- (0 + 0.2, -0.5 - 0.2) -- (0 + 0.2, -0.5 + 0.2) -- (0 - 0.2, -0.5 + 0.2) -- (0 - 0.2, -0.5 - 0.2); \draw (0 - 0.2, -1 - 0.2) -- (0 + 0.2, -1 - 0.2) -- (0 + 0.2, -1 + 0.2) -- (0 - 0.2, -1 + 0.2) -- (0 - 0.2, -1 - 0.2); \end{tikzpicture} \hfill \end{center} \begin{solution} \textbf{1:} Single error at position \texttt{1010} \par \textbf{2:} Double error \par \textbf{3:} No error \par \end{solution} \vfill \pagebreak \problem{} Analyze this coding scheme. \begin{itemize} \item Can we detect one single-bit errors? \item Can we detect two single-bit errors? \item What errors can we correct? \end{itemize} \vfill \problem{} Which message bits does each pairity bit cover? \par In other words, which message bits affect the value of each pairity bit? \par \vspace{1mm} Four diagrams are shown below. In each grid, fill in the bits that affect the shaded pairity bit. \begin{center} \hfill \begin{tikzpicture}[scale = 1.25] \draw (-0.25, 0.25) -- (1.75, 0.25); \draw (-0.25, -0.25) -- (1.75, -0.25); \draw (-0.25, -0.75) -- (1.75, -0.75); \draw (-0.25, -1.25) -- (1.75, -1.25); \draw (-0.25, -1.75) -- (1.75, -1.75); \draw (-0.25, 0.25) -- (-0.25, -1.75); \draw (0.25, 0.25) -- (0.25, -1.75); \draw (0.75, 0.25) -- (0.75, -1.75); \draw (1.25, 0.25) -- (1.25, -1.75); \draw (1.75, 0.25) -- (1.75, -1.75); \draw[pattern=north east lines] (0.5 - 0.2, 0 - 0.2) -- (0.5 + 0.2, 0 - 0.2) -- (0.5 + 0.2, 0 + 0.2) -- (0.5 - 0.2, 0 + 0.2) -- (0.5 - 0.2, 0 - 0.2); \end{tikzpicture} \hfill \begin{tikzpicture}[scale = 1.25] \draw (-0.25, 0.25) -- (1.75, 0.25); \draw (-0.25, -0.25) -- (1.75, -0.25); \draw (-0.25, -0.75) -- (1.75, -0.75); \draw (-0.25, -1.25) -- (1.75, -1.25); \draw (-0.25, -1.75) -- (1.75, -1.75); \draw (-0.25, 0.25) -- (-0.25, -1.75); \draw (0.25, 0.25) -- (0.25, -1.75); \draw (0.75, 0.25) -- (0.75, -1.75); \draw (1.25, 0.25) -- (1.25, -1.75); \draw (1.75, 0.25) -- (1.75, -1.75); \draw[pattern=north east lines] (1 - 0.2, 0 - 0.2) -- (1 + 0.2, 0 - 0.2) -- (1 + 0.2, 0 + 0.2) -- (1 - 0.2, 0 + 0.2) -- (1 - 0.2, 0 - 0.2); \end{tikzpicture} \hfill \begin{tikzpicture}[scale = 1.25] \draw (-0.25, 0.25) -- (1.75, 0.25); \draw (-0.25, -0.25) -- (1.75, -0.25); \draw (-0.25, -0.75) -- (1.75, -0.75); \draw (-0.25, -1.25) -- (1.75, -1.25); \draw (-0.25, -1.75) -- (1.75, -1.75); \draw (-0.25, 0.25) -- (-0.25, -1.75); \draw (0.25, 0.25) -- (0.25, -1.75); \draw (0.75, 0.25) -- (0.75, -1.75); \draw (1.25, 0.25) -- (1.25, -1.75); \draw (1.75, 0.25) -- (1.75, -1.75); \draw[pattern=north east lines] (0 - 0.2, -0.5 - 0.2) -- (0 + 0.2, -0.5 - 0.2) -- (0 + 0.2, -0.5 + 0.2) -- (0 - 0.2, -0.5 + 0.2) -- (0 - 0.2, -0.5 - 0.2); \end{tikzpicture} \hfill \begin{tikzpicture}[scale = 1.25] \draw (-0.25, 0.25) -- (1.75, 0.25); \draw (-0.25, -0.25) -- (1.75, -0.25); \draw (-0.25, -0.75) -- (1.75, -0.75); \draw (-0.25, -1.25) -- (1.75, -1.25); \draw (-0.25, -1.75) -- (1.75, -1.75); \draw (-0.25, 0.25) -- (-0.25, -1.75); \draw (0.25, 0.25) -- (0.25, -1.75); \draw (0.75, 0.25) -- (0.75, -1.75); \draw (1.25, 0.25) -- (1.25, -1.75); \draw (1.75, 0.25) -- (1.75, -1.75); \draw[pattern=north east lines] (0 - 0.2, -1 - 0.2) -- (0 + 0.2, -1 - 0.2) -- (0 + 0.2, -1 + 0.2) -- (0 - 0.2, -1 + 0.2) -- (0 - 0.2, -1 - 0.2); \end{tikzpicture} \hfill \end{center} \problem{} How many pairity bits does each message bit affect? \par Does this correlate with that message bit's index? \vfill \problem{} Say we have a message with exactly one single-bit error. \par If we know which parity bits are inconsistent, how can we find where the error is? \vfill \pagebreak \problem{} How efficient is the 16-bit hamming code? \vfill \problem{} Can you generalize this system for messages of 4, 64, or 256 bits? \vfill \problem{} How efficient is each code in \ref{generalize-hamming}? \par What do we sacrifice for this efficiency gain? \vfill \pagebreak