【发布时间】:2014-09-21 23:09:39
【问题描述】:
所以我正在尝试解决这个家庭作业,但我已经被这个特殊问题困扰了几个小时,无法弄清楚。我觉得我很亲近!但是后来我更改了代码中的某些内容,而其他内容不正确..
/*
* logicalShift - shift x to the right by n, using a logical shift
* Can assume that 0 <= n <= 31
* Examples: logicalShift(0x87654321,4) = 0x08765432
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 20
* Rating: 3
*/
int logicalShift(int x, int n) {
int move;
int y;
y = x >> n;
y = ~y << 1;
move = (y & (x >> n));
return move;
}
这里缺少什么?我得到 0x80000000 >> 31 为 0 但应该是 1 - 但除此之外我不知道..
【问题讨论】:
标签: c bit-manipulation bitwise-operators integer-arithmetic