Pass the integer 2 to this function, and then return an integer equal to 4
x = 2;
x = rotateInt('L', x, 1);
(left shift by 1 shift)
Example: 00000010 → turn left by 1 → 00000100
but if I pass this:
x = rotateInt('R', x, 3);
he will return 64, 01000000
Here is the code, can someone fix the error ... thanks
int rotateInt(char direction, unsigned int x, int y)
{
unsigned int mask = 0;
int num = 0, result = 0;
int i;
for (i = 0; i < y; i++)
{
if (direction == 'R')
{
if ((x & 1) == 1)
x = (x ^ 129);
else
x = x >> 1;
}
else if (direction == 'L')
{
if ((x & 128) == 1)
x = (x ^ 129);
else
x = x << 1;
}
}
result = (result ^ x);
return result;
}
source
share