Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Why not ...

  x, y = minmax(x, y)
  return x + (y - x) / 2;

?


There's branching involved in the minmax operation, so it'll always be slower than (x|y) - ((x^y)>>1).


    min = x^((y^x)&-(y<x));
    return min + ((y^x^min)-min)/2;
Still less efficient.


(-0x80000000, 0x7fffffff)


y - x overflows on y = INT_MAX and x = -1.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: