In this project you will write an encoder and decoder for a single-error-correcting Hamming code. Your encoder will encode each byte of the original input stream into a 12-bit word (8 data bits and 4 error correcting bits). The decoder will decode each 12-bit word in the stream back into a byte, in such a way that if any one bit of the 12-bit word was flipped, then the original byte is recovered. Write both the encoder and decoder in C++. Recall that certain aspects of C++ are implementation dependent, so the official platform for this project is g++ on Euclid (this is where I will compile and run your code - don't assume that your compiler is 100% the same as g++ on Euclid). For example, right shift can be slightly different on different platforms: the C++ language specification is mute on whether a 1 or 0 is shifted into the uppermost bit if it is already a 1.
You are to write programs
decode that meet all of the following requirements. Read them all before starting your work.
encodereads bytes from standard input and writes corresponding 12-bit words (which includes 4 error-correcting bits) to standard output, using a single-error-correcting Hamming code. The way in which you encode each 12-bit word is up to you, however, don't use more bytes than necessary. For example, if
encodewas run on a file containing 40 bytes of data, it would produce 60 bytes of output; 41 bytes of input would result in 62 bytes of output, with 1 half-byte (also known as a nibble) being unused.
decodereads a stream of bytes from standard input (which encode the 12-bit words) and writes the corresponding bytes to standard output, using a single-error-correcting Hamming code. It must be able to recover from any single-bit error in each 12-bit word.
decodemust be able to recover from any errors introduced by this program, which basically puts a single bit of error in every other byte. For example, the following command copies any binary file
finalData, without errors (even if
rawDatahas an odd number of bytes):
./encode < rawData | ./scramble | ./decode > finalData
diff rawData finalData
makeshould compile your code, producing both
Hint: I believe it's easiest if you have
encode write its output in 3-byte units: Write one byte directly from the input stream, followed by one byte worth of ECC, followed by another byte from the input stream. The ECC byte contains the ECC bits for the surrounding bytes. This makes for a lot less "bit-busting", as opposed to, say, writing a class that is capable of taking any bit stream and writing it out as a byte stream.
The project is due at the beginning of class on Friday, October 29, 2010. Be sure to give your entire program a good proofread before handing it in; even small errors will result in me handing it back for another iteration.