/*
* ezThreeFourths - multiplies by 3/4 rounding toward 0,
* Should exactly duplicate effect of C expression (x*3/4),
* including overflow behavior.
* Examples: ezThreeFourths(11) = 8
* ezThreeFourths(-9) = -6
* ezThreeFourths(1073741824) = -268435456 (overflow)
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 12
* Rating: 3
*/
int ezThreeFourths(int x) {
int z = x+x+x;
int sign_z = z>>31;
return ((z>>2)&(~sign_z)) + (((z>>2)+1)&sign_z);
}
Я пытался решить эту загадку, но
ERROR: Test ezThreeFourths(-2147483648[0x80000000]) failed... ...Gives -536870911[0xe0000001]. Should be -536870912[0xe0000000]
скомпилирован с помощью gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-51)
Что не так с этим решением?