News for hackers. Stuff that matters.
Re:Signed Integers Are Two’s Complement by caf on Thursday May 31, 2018 @12:05AM (#17193860)
No?

If x is 3, then (x * 2) / 2 is also equal to 3 (as per the GP) but (x & ~1 * 2) / 2 is equal to 0.

(If you meant ((x & ~1) * 2) / 2, then that is equal to 2).

Re:Signed Integers Are Two’s Complement by cornholio on Thursday May 31, 2018 @03:32AM (#17194617)
You are of course correct. Not only have I mistook precedence, but I confused the lower bit with the upper one where overflow occurs.

So the compiler optimizable fix is something like: ((x & INT_MAX) * 2) / 2 = x

Re:Signed Integers Are Two’s Complement by caf on Thursday May 31, 2018 @03:54AM (#17194708)

  ((x & (INT_MAX / 2)) * 2) / 2

?
Re:Signed Integers Are Two’s Complement by cornholio on Thursday May 31, 2018 @04:25AM (#17194830)
My version works up to x = INT_MAX and your version still fails for negative ints, so I don't see the benefit of restricting the range. Getting closer :)