Travis said:
min(a,b)=(1/2)*abs(a+b)-(1/2)*abs(a-b)
If abs() is allowed, and if overflow can be ignored, this should always
work:
min(a,b) = (a+b-abs(b-a))/2
If abs() is considered cheating, then |n| can easily be accomplished without
a compare if we know where the sign bit is in n. For 32-bit signed integers
|n| is n*(2*(n>>31)+1). I believe that will work for sign-magnitude as well
as the usual (these days) twos-complement. This of course has overflow
holes in it (e.g. if fails when n is -0x80000000 on a two's complement
machine). And it assumes that >> propagates sign. One could just as easily
use a mask to pick out the sign bit s, then compute u*s+v, where u and s are
chosen so that the two possible results are +1 and -1, then multiply that by
n.
Nearly all the overflow issues can be avoided if you first compute variables
to hold the signs of a and b (based on above technique) and then use these
to create a sum in which all subtotals are zero (because they are multiplied
by something that is zero) except the correct answer.
Bob H
P.S. Note that |n| is intended to mean 'absolute value of n'.