Hamming Code Project

by: burt rosenberg
at: university of miami


The Hamming(7,4) Code from
the Wolfram Demonstrations Project
Contributed by: Jacob A. Siehler
This project will familiarize you with the important data link tool of error correcting and error detecting codes by experimenting with the Hamming Code.

Specific Objectives

Man Page

NAME
   myhamming --- hamming encode/decode a binary data stream, using (7,4) Hamming with Parity
      
SYNOPSIS

   myhamming [-e|-d] [-v] [-D n]

DESCRIPTION

   Takes bytes from standard in and write bytes to standard out. The bytes and bits are
   numbered as so:
   
       +---+---+---+---+---+---+---+---+
       | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 |        Input Byte
       +---+---+---+---+---+---+---+---+
            
            First Output Byte                       Second Output Byte
       +---+---+---+---+---+---+---+---+    +---+---+---+---+---+---+---+---+
       | A | B | 8 | C | 7 | 6 | 5 | X |    | D | E | 4 | F | 3 | 2 | 1 | Y |
       +---+---+---+---+---+---+---+---+    +---+---+---+---+---+---+---+---+

   Where A, B and C are Hamming bits corresponding to parity signatures 001, 010 and 100, 
   and 8, 7, 6 and 5 are the data bits from the input byte corresponding to parity
   signatures 011, 101, 110, and 111. X is the overall parity bit for the first output
   byte, parity even. Likewise for D, E and F parity bits protecting data bites 4, 3, 2
   and 1, and Y the overall parity bit protecting the second byte.
   
   The left most byte is written to standard out first on encode, and read from standard 
   in first on decode.
   
   If the 7 bits of the Hamming portion show no error, and the overall parity bit is 
   ignored and the output data bits are 8, 7, 6, 5 (4, 3, 2, 1). 
   
   If the 7 bits of the Hamming portion show an error, and the overall parity bit X (Y) 
   shows no error, it is assumed that two bit errors have occurred, and the program aborts.
   
   In other cases, the Hamming Code parity bits A, B, and C (D, E and F) are used to 
   correct the one bit of error in 8, 7, 6, 5 (4, 3, 2, 1).
   
   
OPTIONS

  -e encode the data stream. The default if no flag specified.
  -d decode the data stream.
  -v verbose
  -D debug level, takes an positive integer 0 (default) through [your choice]. 

RETURNS
  
  Returns 0 if not errors, or only 1 bits errors. Halts immediately on a 2 bit
  error or uneven number of input bytes on decode, and returns -1.
  
AUTHOR

  You!

Detailed Directions

Refer to: for instruction on Hamming Codes. Be careful about how the bits and bytes are numbered. I have tried to make the pictures tell the story. In the picture of an 8 bit byte, which I numbered with bit 8 to the leftmost, that bit is also known as 0x80, and bit 1 is also known as 0x01.

Copy the files from [class-repo]/class/proj1 into [class-repo]/[username]/proj1 directory, add, commit, make clean, make, make test.

  1. Your first goal is to write the functions in myfunction.c to past the test-encode target.

  2. Your second goal is to write myhamming.c to pass the test-hamming target.

The Makefile

Study the provided Makefile. No project is correct without a correct Makefile. The grader and I expect that we can:

svn update; make clean; make ; make test
and be exactly where you are on the project. Same code, same headers, same compiler options, same test case, same bug. Do not check in binaries or other build products! Points off for unnecessary stuff in the repo.

If you know nothing of makefiles, find a tutorial and read it. The make concept is there are targets, which depend on dependencies. If any of the dependencies change, the target must be rebuilt to include those changes. This is done by checking the date stamps of the target and dependences (and this can sometimes lead to trouble).

The actions are the sequence of commands to run to rebuild the target. Make works recursively to determine if any of the dependencies are out of date, and rebuilds those first.

The syntax is:

<TARGET>: <DEPENDENCY-1> < DEPENDENCY-2>
[TAB]	  <ACTION-1>
[TAB]	  <ACTION-2>
Note that it is a tab, not a space, that starts each action line. The first empty line ends the sequence of actions.
# sample makefile.
# everything after a # is a comment

# this is a makefile variable.
ME= my-executable
MS= my-subroutines

my-executable: my-executable.c my-executable.h my-subroutines.o
	cc -o my-executable my-executable.c my-subroutines.o
          
my-subroutines.o: my-subroutines.c my-executable.h
	cc -c my-subroutines.c
          
test:
	@echo The answer is 42.
	
clean:
	-rm ${MS}.o ${ME}
	
submit:
	make clean
	svn commit -m "Submitted for grading"

Other notes

See the Projects tab on the home page for information about platform preparation: the lab account, working directly on a Mac OSX machine, working on Ubuntu on a virtual machine, or working on a cloud AWS image.

The use of a parity bit allows us to identify two bit errors. With two bits of error, at least one error must be in the data, and if two bits are in the data, they cannot cancel. Hence the hallmark of two bits of error is correct Bit Parity, and incorrect Hamming Parity. If we knew that one bit error was the Bit Parity, we could correct the data bits, but we cannot know this.

Further than two bit errors, the results can misinterpreted as fewer error bits than had actually occurred.

ErrorsHPBPActionComments
0AcceptNo errors
1BP discardedParity bit was inverted
1HB corrects errorHamming code corrects one bit error
2RejectOne or two bit errors in Hamming Code
3fallacious correctionFailure - looks like one bit error
3fallacious correctionFailure - looks like one bit error
4AcceptFailure - looks like no errors
4RejectLucky correct action

This project is less than 100 lines of code. If it takes up to 300 lines of code, that's probably normal. If you start hitting 500 lines of code, you might want to reconsider your approach. Here is the edited the output of wc run in the class directory ("before") and in a solutions directory ("after").

LOC beforeLOC afterFile
5953Makefile
5669functiontest.c
2020functiontest.ref
70129myfunctions.c
6379myhamming.c
2727myhamming.h
11myhamming.ref
296378total
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.

Author: Burton Rosenberg
Created: January 17, 2015
Last Update: January 17, 2015