1

this is just for fun.

Can you give me a way to determine the greater value of two numbers? You can use other operators except greater than or less than operator.

Use any tool you like (programming language, pencil paper, etc.). And another thing you cannot do the following:

int main()
{
    int num1, num2;
    cin >> num1 >> num2;

    if( (num1-num2) + abs(num1-num2) )
        cout << num1 << " is bigger" << endl;
    else 
        cout << num2 << " is bigger" << endl;
    return 0;
}
phuclv
  • 37,963
  • 15
  • 156
  • 475
  • 1
    Great question, but I'm not sure stackoverflow is the right place to ask it. Maybe this would be better-suited for programmers.SE.com? – blubb May 17 '11 at 14:04
  • 1
    sounds like fun homework – Aducci May 17 '11 at 14:04
  • Your sample won't provide the correct answer, even if it were allowed. – Jim Mischel May 17 '11 at 14:05
  • 1
    All you have to do is a loop adding `1` to `num1` until it equals `num2` or `INT_MAX` ;-) Anything more complex violates the principle of only performing complex optimisation when there is a demonstrated need! – Keith May 18 '11 at 03:16
  • Possible duplicate of [How do I programmatically return the max of two integers without using any comparison operators and without using if, else, etc?](https://stackoverflow.com/questions/227383/how-do-i-programmatically-return-the-max-of-two-integers-without-using-any-compa) – phuclv Sep 29 '17 at 07:16

8 Answers8

3

Well, if you assume two's complement arithmetic:

int highBit = ~INT_MAX;
int rslt = num1 - num2;

if (rslt & highBit)
    // num2 > num1
else if (rslt)
    // num1 > num2
else
    // num1 == num2

This will only work when both numbers are positive. For example, if num1 is positive and num2 is negative, then num1-num2 could overflow, causing the result to be negative, which would erroneously report that num2 > num1. If both numbers are negative, this will report the opposite (i.e. -12 will be reported greater than -1).

Jim Mischel
  • 131,090
  • 20
  • 188
  • 351
2
#include <algorithm>

cout << std::max(num1, num2) << " is bigger" << endl;
Paul R
  • 208,748
  • 37
  • 389
  • 560
  • I would argue that that is using the greater than or less than operator. – GWW May 17 '11 at 14:06
  • 4
    @GWW: that depends on how std::max is implemented, about which we can make no assumptions of course. ;-) – Paul R May 17 '11 at 14:07
2

Ok, I would transform them to binary code and walk from the left byte-by-byte. Whichever first has 1 when the other one has not (means: has 0), this is greater.

So if you go from left to right and:

  1. both have 0 at the current position: go to the next position,
  2. both have 1 at the current position: go to the next position,
  3. first one has 1 at the current position and the second one has 0: the first one is bigger,
  4. second one has 1 at the current position and the first one has 0: the second one is bigger,

If you determine that 3. or 4. is matched, you have the result. If 1. or 2. is matched, repeat the same for the next position. If you have walked through all the bytes and did not determine that one of them is bigger, then both are equal.

Tadeck
  • 132,510
  • 28
  • 152
  • 198
1

Logarithm of a negative value is undefined; different languages/frameworks process it differently. This solution is for C#:

using System;
public class Test
{
    public static bool gt( double a, double b ) {
        return Double.IsNaN(Math.Log(b - a));
    }
    public static void report_gt( double a, double b) {
        if( gt(a,b) )
            Console.WriteLine("{0} is greater than {1}", a, b);
        else
            Console.WriteLine("{0} is less than or equal to {1}", a, b);
    }
    public static void Main()
    {
        Test.report_gt(-1, 0);
        Test.report_gt(1, 0);
        Test.report_gt(1, 2);
        Test.report_gt(-1, -2);
    }
}

Output:

-1 is less than or equal to 0
1 is greater than 0
1 is less than or equal to 2
-1 is greater than -2

A similar solution for C could use floating point exceptions. Unfortunately, C++ does not throw an exception for a negative argument to log2() for a nice try-catch solution in this fun contest :).

phuclv
  • 37,963
  • 15
  • 156
  • 475
Alexey Kukanov
  • 12,479
  • 2
  • 36
  • 55
0

This works for positive integers:

#! /usr/bin/python
import math

def bigger (a, b):
    length = int (math.log (a, 2) + math.log (b, 2) + 1)
    a = toFixedBitString (a, length)
    b = toFixedBitString (b, length)
    print a, b
    while a:
        if a [0] == '1' and b [0] == '0':
            print "First number is bigger."
            return
        if a [0] == '0' and b [0] == '1':
            print "Second number is bigger."
            return
        a = a [1:]
        b = b [1:]
    print "Numbes are equal."

def toFixedBitString (a, length):
    retVal = ''
    for x in range (length): retVal = ['0', '1'] [a >> x & 1] + retVal
    return retVal
Hyperboreus
  • 136
  • 1
0

Warning: Untested code.

num2 ~= num2;
++num2;

num1 += num2;

rotate_left(num1, 1);

if (num1 == 0)
    std::cout << "num1 == num2";
else if (num1 & 1) 
    std::cout << "num1 < num2";
else
    std::cout << "num1 > num2";

I haven't thought about it a lot, but there are probably combinations that will fail due to overflow.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
0

SSE 4.1 solution

#include <smmintrin.h>
#include <iostream>

using namespace std;

int main()
{
    int num1, num2, test;
    __m128i v1, v2, vcmp;

    cin >> num1 >> num2;
    v1 = _mm_set1_epi32(num1);
    v2 = _mm_set1_epi32(num2);
    vcmp = _mm_cmpgt_epi32(v1, v2);
    test = _mm_testz_si128(vcmp, vcmp);
    if (test == 0)
    {
        cout << "num1 is bigger" << endl;
    }
    else
    {
        cout << "num2 is bigger" << endl;
    }
    return 0;
}

$ g++ -Wall -msse4.1 cmpgt.cpp -o cmpgt
$ ./cmpgt
-10 10
num2 is bigger
$ ./cmpgt
10 -10
num1 is bigger
Paul R
  • 208,748
  • 37
  • 389
  • 560
0
    #include <limits>

    int a = -1;
    int b = -11;

    // true for positive difference
    bool dpos = ~(unsigned int)(a - b) >> numeric_limits<int>::digits; // 31 for 32-bit

    cout << "Problem : a = " << a << " and b = " << b << endl;
    if (a == b) 
         cout << " a == b " << endl;
    else if (dpos) 
         cout << " a > b " << endl;
    else 
         cout << " a < b " << endl;
Nikiton
  • 268
  • 1
  • 7