CMSC 341H Data Structures Fall 2005

Project 1

Complete and submit project 1 as described on the main 341 web site subject to the following modification.

Do not answer the questions in the file named 341-Fall05-proj1_questions.txt as the project description requests. Rather, recall that we discussed a templated class that computes factorial values at compile time. You are to write a templated class that determines whether an unsigned short int, when viewed as a bit string, is a palindrome. For example, the integer 0xB00D as a bitstring is 1011000000001101, which reads the same forward and backward and is thus a palindrome. I should be able to use your class as follows:

  cout << Palindrome<0xB00D>::value << endl;
That is, the class is instantiated with a single unsigned short int argument, and its public static const bool data member value is set to true if the argument is a palindrome, otherwise it is set to false.

HINT: The solution I came up with involved writing a templated helper class as follows:

  template<unsigned short int left, unsigned short int right>
  class PalindromeHelper {
    /* Your code goes here */
  };
This class takes the left-most byte and the right-most byte of an unsigned short int as arguments and sets its value to be true if the corresponding unsigned short int is a palindrome. The Palindrome class sets up the call to the PalindromeHelper class which calls itself recursively.

This is going to be a challenge. Feel free to ask me for additional hints. You may also find the following web page on template metaprogramming to be useful. Instead of submitting your answers to the original project questions in 341-Fall05-proj1_questions.txt, submit your Palindrome code in that file. If you don't get your code working, submit what you have along with a short (one or two paragraphs) description of the problems you ran into and how you tried to solve them. You can get full credit without getting the code to work as long as it's apparent that you learned something along the way.