int x = 2;
x = rotateInt('L', x, 1); // should return 4
x = rotateInt('R', x, 3); // should return 64
这里是代码,有人可以检查它,让我知道错误是什么?位移C
编译成功,但它执行时说Segmentation Fault
。
int rotateInt(char direction, unsigned int x, int y)
{
int i;
for(i = 0; i < y; i++)
{
if(direction == 'R')
{
if((x & 1) == 1)
{
x = x >> 1;
x = (x^128);
}
else
x = x >> 1;
}
else if(direction == 'L')
{
if((x & 128) == 1)
{
x = x << 1;
x = (x^1);
}
else
x = x << 1;
}
}
return x;
}
如果你已经完成了10分钟前的相关问题http://stackoverflow.com/questions/3928659/rotating-bits-of-any-integer-in-c你可能想在移动之前接受一个答案上。 – Dusty 2010-10-14 00:20:48