3 Jan
2012
3 Jan
'12
7:55 a.m.
On a 32-bit machine in C where x,y are ints, min(x,y) = y+(((x-y)>>31)&(x-y)); max(x,y) = x-(((x-y)>>31)&(x-y)); A related cute one is where you have X with 0<=X<M then you add some alteration to X which might move it into the interval [M, 2M) and we want to take the result mod M so as to return to the interval [0, M). Slow way: X = X%M. Faster way: if(X>=M) X -= M; no-if way: X -= ((M-1-X)>>31)&M; If X due to a subtractive alteration might have moved into [-M, 0) so to get it back you add M, if(X<0) X += M; no if-way: X += (X>>31)&M;