To find the maximum of two numbers without a judgment statement

Source: Internet
Author: User
Tags abs hash

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.