Although this is not a great way to do it, your logic does seem correct. The problem I am having with your code is inputing the hex number. I added
cin.setf(ios::hex, ios::basefield); which should allow you to enter hex numbers, and it does for small numbers, but seems to choke with large numbers... not sure what the problem there is.
As for rotating bits. There is a better way. Lets say I wanted to rotate a byte worth of data 3 places to the left:
byte = 1100 0011
temp = byte >> (8 - 3) = byte >> 5 = 0000 0110
byte = byte << 3 + temp = 0001 100 + 0000 0110 = 0001 1110
What we do is use a temp variable to hold the bits that would have gotten erased in the shift, then we add them back in.
here is an example of a right rotate
CODE
int RotateBitsRight(int num, int numBits)
{
int temp;
int wordSize = 8 * sizeof(int); //Calculate how many bits in an int
int Shift = wordSize - numBits;
temp = (unsigned)num << Shift;
num = ((unsigned)num >> numBits) + temp;
return num;
}
I will look into the input... not sure what is wrong off the top of my head.