NM posted:

I am given an int and have to tell whether the bit representation of

that int is a palindrome or not.

Many people have written a template function which determines whether an

array is a palindrome. If you want to be fancy, you can define your own

class called BitHandle which works exactly like a pointer, and then pass

this object to the pre-written (and maybe even very efficient) palindrome

function. Something like:

Warning: Buggy, sloppily written code...

#include <limits>

template<class T>

class BitHandle {

private:

T const val;

unsigned bit_index;

public:

BitHandle(T const val, unsigned const i)

: val(arg), bit_index(i) {}

BitHandle &operator++() { ++bit_index; return *this; }

BitHandle &operator--() { --bit_index; return *this; }

operator bool() const

{

return val & T(1)<<bit_index;

}

};

int main()

{

unsigned val = 72;

TemplatePalindromeFunction(

BitHandle(val,0),

BitHandle(val,std::numeric_limits<unsigned>::digit s) );

}

bool isIntPalindrome(int num)

{

unsigned int temp = num;

int reversed = 0;

while (num != 0) {

reveresed <<= 1;

Here you shift zero to the left by one place. That will have no effect

whatsoever.

reversed |= (temp & 1);

Here you test if "temp" has its low-order bit set, and it does, then you

set reversed's low-order bit.

temp >>= 1;

You shift temp once to the right, effectively dividing it by 2.

--

Frederick Gotham