CSAPP-3e-Solutions
CSAPP-3e-Solutions copied to clipboard
CSAPP-3e-Solutions/chapter2/code/threeforths.c 你的方法不大直观。
2.80
int threefourths(int x)
{
int is_pos = ~x & INT_MIN;
int has_remainder = x & 3;
int fourth = x >> 2;
// 为了保证 threefourths 向零舍入
// fourth 必须与 向零舍入 相反
// 所以, 正数不能整除 4 的时候, fourth++
is_pos &&has_remainder && (fourth++);
// 负数右移 2 位自带此效果
return x - fourth;
}
PS: 文件名应该是 threefourths.c