## ramya_bugbuster 1

hi friends..
how to compare 3 number without using relational operator?
do u have any idea?

## stephen84s 550

yes I do, but can I see what you have tried, cause this looks like one of those questions with little practical application and mostly for academic use only ?

commented: Well said, it is a pratical question. Remeber me doing in the university. haha +2

## ssharish2005 62

Haha, OK i will give you an hint but not the answer. So relational operator should be used. But they dint say that cant use arithmetically operator did they???? Is that more than enough to give you a hint.

ssharish

## jephthah 1,888

you can't use any of the relational operators?

>, >=, <, <=, ==, and !=

well, then, what do you have left that could possibly be of use?

bitwise and logical operators.

## ramya_bugbuster 1

The question of how to compare three numbers without using relational operator.

The following peace of code helps to compare two numbers the same can be extended to three variables.
hi..
i got this reply from my friend..
i didnt check this yet now..
if u like just try..

The logic used is given two numbers if a - b is < 0 then b > a else a > b;
The negativity is checked based on the left most bit. If it is set to 1 then it is negative or it is positive.

Any other doubts or if there is a better answer please reply.

``````int max1(int a, int b)
{
int chxvar = 1 << (8 * sizeof(int) - 1); //This initialises to a integer
if( (a - b) & chxvar)                            //of the value(100000000..)

return b;
else
return a;
}
``````

## ssharish2005 62

I don't really see a great importance of this code here

``int chxvar = 1 << (8 * sizeof(int) - 1);``

any explanations???

ssharish

## stephen84s 550

``````int max1(int a, int b)
{
int chxvar = 1 << (8 * sizeof(int) - 1); //This initialises to a integer
if( (a - b) & chxvar)                            //of the value(100000000..)
return b;
else
return a;
}
``````

From the code I get the hint you are checking the sign bit here, this solution would not always work. For example consider the situation where `a = MAX_INT_VALUE; and b=MIN_INT_VALUE;` In such a scenario (a-b) would be equal to "-1" which is less than zero, due to overflow.

Note:

``````MAX_INT_VALUE and MIN_INT_VALUE mentioned only for illustration,
For my compiler (GCC 4.1.2) they are
MAX_INT_VALUE=2147483647
MIN_INT_VALUE=-2147483648
``````

## stephen84s 550

I don't really see a great importance of this code here

``int chxvar = 1 << (8 * sizeof(int) - 1);``

any explanations???

ssharish

If an `int` variable is set to "1", the very last bit is set to 1 and rest all bits are 0, `1 << (8 * sizeof(int) - 1)` moves that 1 bit to the very start of memory location of that `int` variable, so it can be directly anded with `(a-b)` , to check if the sign bit is set or not.
Theres another draw back I just spotted here, if a=b, a-b=0 therefore the sign bit will not be set and the function will return saying a is greater than b.

## Prabakar 77

nice catch stephen84s. Then I guess the only way should be to play with bitwise operator

## jephthah 1,888

ramyavairamani's solution is correct, although not entirely complete as stephen84s showed.

all you have to do to avoid the overflow, is by first checking if either "a" or "b" is negative. if one is negative and one is not, simply return the one that is positive as the greater value

if both are negative, or if both are positive, then ((a-b) & MIN_INT_VALUE) will indicate which one is greater, as previously demonstrated, and without the danger of overflow.

.