Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/articles/c-tutorials/)
-   -   To find a>b or a<b without using of Logical & relational Operators!!! (http://www.go4expert.com/articles/ab-ab-using-logical-relational-operators-t8223/)

To find a>b or a<b without using of Logical & relational Operators!!!

Program to find max(x,y) or Min(x,y) without using any relational and logical operator.

Code: C

`int maximum(int x, int y) {      int fNum[2],temp,num[2];    num[0] = x;     num[1] = y;       temp=fNum[ Fun4Sign(x - y)];      return( temp); }int Fun4Sign(int sgn) {        int temp;        temp= unsigned(sgn) >>  (sizeof(int)*8-1);    return(temp); }`

EXTRA DOSE: One More thing to find min and max...
Code: C

`int  MinimumOf( int a, int b)  {    return(   b + ((a - b) & ((a - b) >> (sizeof(int) * CHAR_BIT - 1)))  );}`

Code: C

`int  MaximumOf( int a, int b)  {    return(   a - ((a - b) & ((a - b) >> (sizeof(int) * CHAR_BIT - 1)))  );}`

 shabbir 12Jan2008 21:45

Re: To find a>b or a<b without using of Logical & relational Operators!!!

Looks like there is some problem in your code where you have not defined num

 oogabooga 14Jan2008 01:40

Re: To find a>b or a<b without using of Logical & relational Operators!!!

That's pretty neat, the perhaps not universally portable.
Your code does better than just avoiding relational and logical operators,
it also avoids conditional (and loop) statements (including ?: ).
Here's a re-write:
Code:

```#include <limits.h> /* for CHAR_BIT: number of bits in a char */ #define INT_BITS (sizeof(int) * CHAR_BIT) int maximum( int x, int y ) {     int xy[] = { x, y };     return xy[ getSign( x - y )]; /* Index xy by sign of (x-y) */ } int getSign( int n ) { /* Returns sign bit of n */     /* Shift sign into bit position 0 */     return (unsigned)n >> INT_BITS - 1; }```
One could replace int xy[] = { x, y } by int* xy = &x, but
that assumes (minimally) that x has a lower address than y.
the sign bit that may not work on all computers.

I also found your MinimumOf and MaximumOf interesting:
Code:

```    /* Fill n with its sign bit, becoming all 1's or 0's */ #define spread_sign(n) ((n) >> (INT_BITS - 1))     /* Return zero if n is positive, otherwise return n */ #define zero_if_positive(n) ((n) & spread_sign(n)) int minimumOf( int a, int b ) {     return b + zero_if_positive( a - b ); } int maximumOf( int a, int b ) {     return a - zero_if_positive( a - b ); }```

Re: To find a>b or a<b without using of Logical & relational Operators!!!

Quote:
 Originally Posted by shabbir Looks like there is some problem in your code where you have not defined num

Very Very Sorry for this mistake.

correct code will be

Code: C
Code:

```int maximum(int x, int y)  {    int fNum[2],temp,num[2];   num[0] = x;    num[1] = y;      temp=fNum[ Fun4Sign(x - y)];    return( temp); }  int Fun4Sign(int sgn) {      int temp;      temp= unsigned(sgn) >>  (sizeof(int)*8-1);    return(temp); }```

 shabbir 14Jan2008 17:34

Re: To find a>b or a<b without using of Logical & relational Operators!!!

I have updated the above article but learn to use the code block in the posts - Before you make a query

 All times are GMT +5.5. The time now is 10:40.