C / C++ FAQs & Programming Resources - ProkutFAQ : GreatestWithoutRelationalOperator

HomePage Recent Changes Recently Commented Login/Register

How to find greatest of two/three/four numbers without using relational operators ?


Finding greatest of two numbers:
  •     int maxof2(int a,int b)
        {
            return (a + b + abs(a - b))/2;
        }


  • /****************************************************
    Purpuse  : Evaluate the bigger one of two integers.
    Author   : ALNG
    Date     : 2003-03-11
    Original : http://search.csdn.net/Expert/topic/1515/1515035.xml
    **************************************************/


    inline int signof(int i)
    {
        return unsigned(i) >> (sizeof (int) * 8 - 1);
    }
     
    int max(int a, int b)
    {
        int p[2];
        p[0] = a;
        p[1] = b;
       
        return p[signof(a - b)];
    }

Function for finding greatest of three numbers:
    int maxof3(int a, int b, int c)
    {
        return (a + b + c * 2 + abs(a - b) + abs(a + b - c * 2 + abs(a - b))) / 4;
    }


Function for finding greatest of four numbers:
    int maxof4(int a, int b, int c, int d)
    {
        return (a + b + c + d + abs (b - a) + abs(d - c) + abs(a + b - c - d + abs( b - a) - abs(d - c)))/ 4;
    }



Warning: These functions might invoke undefined behavior when it overflows/underflows the integer value in the expression, so better not use these methods in actual applications, just use the relational/comparison operators.


CategoryPuzzles
 Comments [Hide comments/form]
the function abs() uses relational operator to give a-b or b-a...here is another method:
max2(int p,int q)
{
int *a;
char b[][]={"p","q"};
a=(int *)calloc((p+q)*sizeof(int),1);
a[0]=1;
printf("%c",b[a[p/q]]);
}
-- byethost15.com (2006-11-27 14:53:23)
Your method fails when p is positive and q is negative. for example p = 10000, q = -2, in that case it might try to access an invalid location a[-5000].

abs() need not necessarily use relational operator, it can be written without using it as below:

unsigned int abs(int n)
{
unsigned int x = n;
unsigned int m;
m = (1 << (sizeof(int) - 1)) & x;
m = (m - (m >> (sizeof(int) - 1))) | m;
return (x ^ m) + (1 & m);
}
-- SharathAV (2006-11-27 22:22:24)
@sharathAV
sizeof(int) -> sizeof(int) * 8
-- byethost15.com (2006-11-28 23:16:45)
HI
in technical round for wipro ask this Q.
i dint answer that. so i loss that.
-- 61.246.235.196 (2007-04-20 06:16:43)
Easiest code two find greater of three numbers(It will work for negative numbers also)
#include<stdio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter 3 numbers:- ");
scanf("%d%d%d",&a,&b,&c);
if ((a-b)>>15)
{ if ((b-c)>>15)
printf("%d",c);
else printf("%d",b);
}
else
if ((a-c)>>15)
printf("%d",c);
else printf("%d",a);
getch();
}
-- AmitJainCoer191 (2007-07-03 09:29:30)
Amit, your code assumes that integer is always 16 bits, which is a wrong assumption. And it also uses non-standard main declaration, read: http://prokutfaq.byethost15.com/VoidMain .
Your comment will be deleted soon because its irrelevant.
-- SharathAV (2007-07-03 14:09:41)
How about using following code?

int main()
{
int mask, x = 3, y = 9;
mask = (x-y)>>31;
printf("%d\n",( (mask & y) | (~mask & x)) );

return 0;
}
-- 203.91.207.30 (2007-10-12 08:37:03)
Page was generated in 0.0736 seconds