First, using ABS () to find the absolute value
int Getmax (int a, int b)
{
return (A + B + abs (a))/2;
}
Second, calculate positive and negative numbers by bitwise operation
#define SIGNAL_BIT (x) (x >> (sizeof (int) * 8-1) & 1)
int Getmax (int a, int b)
{
int hash[2] = {A, b};
int index = Signal_bit (A-B);
return Hash[index];
}
Third, establish the mapping table,
#define SIGNED_BIT (x) (((x) & 0x80000000) >> 31)
#define VALUE_STUFF (x) (X & 0x7FFFFFFF)
#define VALUE_DIFF (x, y) signed_bit (Value_stuff (x)-Value_stuff (y))
int Getmax (int x, int y)
{
int nums[2][2][2] =
{
X,//000
Y,//001
X,//010
X,//011
Y,//100
Y,//101
X,//110
Y//111
};
int idx0 = signed_bit (x);
int idx1 = Signed_bit (y);
int idx2 = Value_diff (x, y);
return NUMS[IDX0][IDX1][IDX2];
}
X,//000 x is positive, y is positive, x load >=y, then x is the maximum value
Y,//001 x is positive, y is positive, x load <y, then Y is the maximum value
X,//010 x is positive, y is negative, then X is the maximum (no need to consider the load portion)
X,//011 x is positive, y is negative, then X is the maximum (no need to consider the load portion)
Y,//100 x is negative, y is positive, then Y is the maximum (no need to consider the load part)
Y,//101 x is negative, y is positive, then Y is the maximum (no need to consider the load part)
X,//110 x is negative, y is negative, x is load >=y, then x is the maximum value
Y//111 x is negative, y is negative, x load is <y, then Y is the maximum value of four, "wonderful" is the
int Getmax (int x, int y)
{
int XY, YX;
XY = ((x-y) >>) & 1;
YX = ((y-x) >>) & 1;
return xy * y + yx * x + (1-XY-YX) * x;
}
If x>y, xy = 0; XY * y = 0; YX = 1; YX * x = x;
If x<y, xy = 1; XY * y = y; YX = 0; YX * x = 0;
If x=y, xy = 0; YX = 0;
Five, but only more, without the most
int getmax (int x,int y)
{
unsigned int z;
Z= ((x-y) >>31) &1;
Return (1-z) *x+z*y;
}
Six, but what if it overflows?
X y one is positive one negative gets a positive number
int getpositive (int x, int y)
{
unsigned int z;
z = x >> & 1;
Return Z * y + (1-z) * x;
}
int getmaxoverflow (int x, int y)
{
unsigned int z;
z = x ^ y >> & 1;
Return (1-Z) Getmax (x, y) + Z * getpositive (x, y);
}
Seven, x + y; X-y; There's an overflow, that's not necessary.
#define SHIFT (sizeof (int) * 8-1)
int Getmax (int x, int y)
{
unsigned int m, n;
int ary[2] = {x, y};
m = x ^ y;
m = M & ~ (M/2) & ~ (M/4);
n = m | 0x01
Return ary[((x & N) + n/2)/n ^! ( M >> SHIFT)];
}
Methods are more for reference and summary