将整数 2 传递给该函数,然后返回一个整数 4
x = 2;
x = rotateInt('L', x, 1);
(将位左移 1)
示例:00000010 -> 向左旋转 1 -> 00000100
但如果我通过这个:
x = rotateInt('R', x, 3);
它将返回 64, 01000000
这是代码,有人可以纠正错误...谢谢
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;
}