The language I use is C.
I have code as follows:
int sign_x=~(x>>31)+1;
int sign_y=~(y>>31)+1;
int sign=sign_x^sign_y;
return ((!sign)&(!(!(0x80000000&(y+(~x+1)))))+(sign&(!sign_x)));
When I set input x=2147483647[0x7fffffff],y=-2147483648[0x80000000],the result is 0.
I wonder why the result is 0 rather than 1 because the first part is 0 and the second part is 1.When I changed operator “+” to “|”,the result is OK.
Can anybody help me? Thanks
0 + 1 is 1, which means that the operator precedence is different than what you thought it is.
gcc is clever enough to emit a warning for this:
add extra parentheses:
or, since the expression is quite complex, temporarily store parts of the result: