Hacker News new | ask | show | jobs
by nwellnhof 1288 days ago
For signed ints, try

    (x >> 1) + (y >> 1) + (x & y & 1)
This rounds toward negative infinity. Also

    (x >> 1) + (y >> 1) + ((x | y) & 1)  // Rounds toward +Inf
    (x >> 1) + (y >> 1) + (x & 1)        // Rounds up if x is odd
    (x >> 1) + (y >> 1) + (y & 1)        // Rounds up if y is odd
I'd be curious if there's a bit-twiddling trick for rounding toward x or y.