News for hackers. Stuff that matters.
Re:Fast midpoint between two integers without overflow by naasking on Wednesday December 07, 2022 @09:52AM (#33894943)
> Then the majority come up with (a / 2) + (b / 2) until they run the unit tests and realize it's wrong.

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.
Re:Fast midpoint between two integers without overflow by pksadiq on Wednesday December 07, 2022 @10:25AM (#33895396)
> x/2 + y/2 + (x & y & 0x01)

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

Re:Fast midpoint between two integers without overflow by slymon99 on Wednesday December 07, 2022 @10:44AM (#33895646)
Isn't (32 bit) INT_MAX 2^31-1 and INT_MIN -2^31, so this is an acceptable solution (since the decimal average is -0.5)?
Re:Fast midpoint between two integers without overflow by pksadiq on Wednesday December 07, 2022 @11:26AM (#33896288)
> since the decimal average is -0.5

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)

Re:Fast midpoint between two integers without overflow by a1369209993 on Wednesday December 07, 2022 @04:57PM (#33900877)
The question is midpoint, not division, so that's irrelevant in the first place, and even if were division, the standard is wrong.