这里是代码,有人可以检查它,让我知道错误是什么?
编译是成功的,但是当我执行它时,它说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; } int x = 2; x = rotateInt('L', x, 1); // should return 4 x = rotateInt('R', x, 3); // should return 64Here is the code, can someone check it and let me know what the error is?
Compilation is successful, but it says Segmentation Fault when I execute it.
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; }最满意答案
我试图在我的电脑上(MacBookPro / Core2Duo),它的工作。 顺便说一句,你的目标架构是什么? 当您使用C运算符“>>”和“<<”时,一些(许多)处理器执行旋转而不是轮班。
I tried on my computer (MacBookPro / Core2Duo) and it worked. By the way, what's your target architecture ? Some (many) processors perform rotation instead of shifts when you use the C operators ">>" and "<<".
在C中移位(bit shifting in C) 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; } int x = 2; x = rotateInt('L', x, 1); // should return 4 x = rotateInt('R', x, 3); // should return 64Here is the code, can someone check it and let me know what the error is?
Compilation is successful, but it says Segmentation Fault when I execute it.
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; }最满意答案
我试图在我的电脑上(MacBookPro / Core2Duo),它的工作。 顺便说一句,你的目标架构是什么? 当您使用C运算符“>>”和“<<”时,一些(许多)处理器执行旋转而不是轮班。
I tried on my computer (MacBookPro / Core2Duo) and it worked. By the way, what's your target architecture ? Some (many) processors perform rotation instead of shifts when you use the C operators ">>" and "<<".
发布评论