I have two integer numbers. I want to find which number is greater than other one without using relational operators.

Dani AI

Generated

Several good ideas appear above: suggested string conversion, / showed the subtraction/abs trick, and ’s exhaustive test demonstrates where those arithmetic tricks fail. The root cause is signed arithmetic—subtracting or calling abs() can hit values that aren’t representable (signed overflow / abs(INT_MIN)), which is undefined in C. String comparisons are also lexical, slow, and need extra handling for signs/padding.

A portable, safe approach is to do all work in the unsigned domain (no signed overflow) and use bit operations to extract sign/borrow bits. The algorithm:

  • Treat the ints as unsigned bit-patterns.
  • If sign bits differ, the non‑negative value is greater.
  • If sign bits are the same, an unsigned subtraction tells which is larger (the high bit of the unsigned difference signals a borrow).
    This uses no relational operators (<, >, <=, >=) and avoids abs() on signed extremes.

Example implementation (C99+):

#include <limits.h>

int compare_no_rel(int a, int b)
{
    unsigned int ua = (unsigned int)a;
    unsigned int ub = (unsigned int)b;
    const unsigned int bits = sizeof(unsigned int) * CHAR_BIT;
    unsigned int sa = ua >> (bits - 1);
    unsigned int sb = ub >> (bits - 1);
    unsigned int sign_diff = sa ^ sb;

    if (sign_diff)
        return (int)(1 - 2 * (int)sa); /* sa==0 ->  1, sa==1 -> -1 */

    unsigned int udiff = ua - ub;                /* well-defined (unsigned) */
    unsigned int borrow = udiff >> (bits - 1);   /* 1 if ua < ub */
    unsigned int nz = !!udiff;                   /* 1 if not equal */
    return (int)(nz * (1 - 2 * (int)borrow));    /*  1, 0 or -1 */
}

Notes: test this against corner cases (INT_MIN, INT_MAX, equal values). For production code, plain relational operators are clearer; this is mainly useful as a careful, portable solution when the challenge forbids </> or when avoiding signed overflow is required.

Recommended Answers

All 5 Replies

You could use sprintf to convert int numbers to string then compare them with strcmp or memcmp

if (x - y + abs(x-y)) { /* x > y */

;)

Hello...
This is Neigyl...
I'm still a newbie here...
I hope this'll help...

#include <stdio.h>
#include <stdlib.h>

int main ()
{
   int a = 11, b = 10;

   /* We didn't use a relational operator here but the definition
       of max() and min() in stdlib.h uses so. */

   printf ("The greater value is %d", max(a, b));
   printf ("\nThe lesser value is %d", min(a, b));

   /* We didn't use a relational operator here in a sense that such
       operator didn't appear inside the if. */

   if (a - b + abs(a - b))
      printf ("a is greater than b.");
   else
      printf ("a is lesser or equal than b.");

   a = b = 5;

   if (a - b)
      printf ("They are not equal.");
   else
      printf ("They are equal.");

   return 0;
   /* With the 2 scenarios above, we never used the relational operator in our program. */
}

<deleted>

It is often prudent to test around the edges.

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

int cmp1(int a, int b)
{
   return a == b ? 0 : a < b ? -1 : 1;
}

int cmp2(int x, int y)
{
   if ( x - y + abs(x-y) )
   {
      return 1;
   }
   if ( y - x + abs(y-x) )
   {
      return -1;
   }
   return 0;
}

int main(void)
{
   int val[] = { 42, -5, 0, 9, INT_MIN, INT_MAX, INT_MIN + 1, INT_MAX - 1 };
   size_t i, j;
   for ( i = 0; i < sizeof val / sizeof *val; ++i )
   {
      for ( j = 0; j < sizeof val / sizeof *val; ++j )
      {
         printf("[%11d,%11d] : %2d | %2d\n", val[i], val[j], 
                cmp1(val[i], val[j]), cmp2(val[i], val[j]));
      }
   }
   return 0;
}

Let's take a look at places where the output differs.

/* my output
[         42,         42] :  0 |  0
[         42,         -5] :  1 |  1
[         42,          0] :  1 |  1
[         42,          9] :  1 |  1
[         42,-2147483648] :  1 | -1
[         42, 2147483647] : -1 | -1
[         42,-2147483647] :  1 | -1
[         42, 2147483646] : -1 | -1
[         -5,         42] : -1 | -1
[         -5,         -5] :  0 |  0
[         -5,          0] : -1 | -1
[         -5,          9] : -1 | -1
[         -5,-2147483648] :  1 |  1
[         -5, 2147483647] : -1 |  1
[         -5,-2147483647] :  1 |  1
[         -5, 2147483646] : -1 |  1
[          0,         42] : -1 | -1
[          0,         -5] :  1 |  1
[          0,          0] :  0 |  0
[          0,          9] : -1 | -1
[          0,-2147483648] :  1 |  0
[          0, 2147483647] : -1 | -1
[          0,-2147483647] :  1 |  1
[          0, 2147483646] : -1 | -1
[          9,         42] : -1 | -1
[          9,         -5] :  1 |  1
[          9,          0] :  1 |  1
[          9,          9] :  0 |  0
[          9,-2147483648] :  1 | -1
[          9, 2147483647] : -1 | -1
[          9,-2147483647] :  1 | -1
[          9, 2147483646] : -1 | -1
[-2147483648,         42] : -1 |  1
[-2147483648,         -5] : -1 | -1
[-2147483648,          0] : -1 |  0
[-2147483648,          9] : -1 |  1
[-2147483648,-2147483648] :  0 |  0
[-2147483648, 2147483647] : -1 |  1
[-2147483648,-2147483647] : -1 | -1
[-2147483648, 2147483646] : -1 |  1
[ 2147483647,         42] :  1 |  1
[ 2147483647,         -5] :  1 | -1
[ 2147483647,          0] :  1 |  1
[ 2147483647,          9] :  1 |  1
[ 2147483647,-2147483648] :  1 | -1
[ 2147483647, 2147483647] :  0 |  0
[ 2147483647,-2147483647] :  1 | -1
[ 2147483647, 2147483646] :  1 |  1
[-2147483647,         42] : -1 |  1
[-2147483647,         -5] : -1 | -1
[-2147483647,          0] : -1 | -1
[-2147483647,          9] : -1 |  1
[-2147483647,-2147483648] :  1 |  1
[-2147483647, 2147483647] : -1 |  1
[-2147483647,-2147483647] :  0 |  0
[-2147483647, 2147483646] : -1 |  1
[ 2147483646,         42] :  1 |  1
[ 2147483646,         -5] :  1 | -1
[ 2147483646,          0] :  1 |  1
[ 2147483646,          9] :  1 |  1
[ 2147483646,-2147483648] :  1 | -1
[ 2147483646, 2147483647] : -1 | -1
[ 2147483646,-2147483647] :  1 | -1
[ 2147483646, 2147483646] :  0 |  0
*/
commented: Yes indeed +31
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.