/*
* fitsBits - return 1 if x can be represented as an
* n-bit, two's complement integer.
* 1 <= n <= 32
* Examples: fitsBits(5,3) = 0, fitsBits(-4,3) = 1
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 15
* Rating: 2
*/
int fitsBits(int x, int n) {
int r, c;
c = 33 + ~n; //c = 33 + ~n = 33 + (-n - 1) = 32 - n
r = !(((x << c)>>c)^x);
return r;
}
参考自 http://stackoverflow.com/questions/28641540/bitwise-operations-and-shifts-problems
289




被折叠的 条评论
为什么被折叠?



