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