Deleted Comment
Deleted Comment
The C standard says: When integers are divided, the result of the / operator is the algebraic quotient with any fractional part discarded (This is often called ‘‘truncation toward zero’’).
So it should be 0 (as per C standard, not sure what C++ standard says)
Seems like you can cover the corner case easily enough:
x/2 + y/2 + (x & y & 0x01)
A few more operations than the article though so not the most efficient solution.This returns -1 for x = INT_MIN and y = INT_MAX were the answer should be 0 (for an example). so not a correct solution
int mid(int x, int y) {
return (x/2 + y/2) + (1 & x & y);
}
would be a more readable solutionedit: Actually, this fails on mid(INT_MIN, INT_MAX) and possibly other mixed sign values (returns: -1, expected: 0 (or -1 is okay?), where the precise answer is -0.5)
more edit: The C standard says: When integers are divided, the result of the / operator is the algebraic quotient with any fractional part discarded (This is often called ‘‘truncation toward zero’’).
So -1/2 should be 0.
https://github.com/zmwangx/ets
[0] https://www.sadiqpk.org/projects/tis