【发布时间】:2010-10-14 00:17:35
【问题描述】:
int x = 2;
x = rotateInt('L', x, 1); // should return 4
x = rotateInt('R', x, 3); // should return 64
这是代码,有人可以检查一下并告诉我错误是什么吗?
编译成功,但执行时却显示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 分钟前 stackoverflow.com/questions/3928659/… 的相关问题,您可能希望在继续之前接受答案。
标签: c bit-manipulation shift