5

I came across a interview question which reads as follows:

"Write a simple C/C++ Macro to find maximum of two numbers without using std library or ternary operator".

I need your help in solving this. I know this is trivial but I couldn't find it. So, posting it here.

#include<iostream>
#define max(x,y) /*LOGIC HERE*/
using namespace std;

void main()
{
    int a = 98453;
    int b = 66394;  
    cout<<max(a,b);
}
a3f
  • 8,517
  • 1
  • 41
  • 46
TheLearner
  • 439
  • 1
  • 7
  • 10
  • See this [answer](http://stackoverflow.com/questions/4772780/find-the-maximum-of-two-numbers-without-using-if-else-or-any-other-comparison-op?rq=1) and this `c++` code will not compile in `C++` compiler.main should return `int`. – Jayesh Bhoi Sep 19 '14 at 04:23
  • 2
    Remember, an interview is also a way for you to determine if you want to work at that company. If you get such a question, ask a few questions in return: Is this normal style? Do your engineers know the differences between C and C++? How many unfixed warnings are there in your code base? How much time do you spend fixing bug reports from the field in comparison to implementing new features? – MSalters Sep 19 '14 at 08:29
  • 1
    Another one talking about a language called *C/C++* I wonder if I must study that arcane language. Why no ask for a macro which can be used on C and C++ instead of talking about *C/C++*? – PaperBirdMaster Sep 19 '14 at 08:55
  • The better reply to the interviewer is that such a macro should not be used because of sign .vs. non-signed and because of the possibility of different variable types. – user3629249 Sep 22 '14 at 07:07

6 Answers6

26

Use Boolean operations to get 0 or 1 and then just add them up:

#define max(x,y) (((int)((x)<(y)) * (y)) + ((int)((y)<=(x)) * (x)))
MooseBoys
  • 6,641
  • 1
  • 19
  • 43
Rich
  • 640
  • 5
  • 12
5
#include <iostream>

#define max(x, y) [a = x, b = y](){ if (a<b) return b; else return a; }()

int main() {
    using namespace std;

    int a = 10;
    int b = 20;
    cout << max(10, 20);
    cout << max(a, b);
};

a solution just for fun : > compiled with c++14

would blow up if x, y has different types

owensss
  • 121
  • 4
3

#define max(x,y) (x+y + abs(x-y))/2 gives you what you are looking for. This works because abs(x-y) = max(x,y) - min(x,y). So, you can rewrite the expression as follows

(x + y) + abs(x-y) = max(x,y) + min(x,y) + max(x,y) - min(x,y)
                   = 2*max(x,y)

As pointed out in the comments, using abs might violate the conditions what you have asked for.

Pradhan
  • 16,391
  • 3
  • 44
  • 59
  • Can you give us a hint as to how this works? I cannot figure it out. It has been far too long since I studied mathematics. Once I could solve Calculus problems in my head. But now... I am getting old. – Benilda Key Sep 19 '14 at 04:15
  • 4
    `abs()` would be a standard library function. – Michael Burr Sep 19 '14 at 04:20
2
#define max(x, y) x - ((x-y) & ((x-y) >> 31))

This assumes x and y are 32 bit.

This works by the fact that the most-significant bit of a negative integer is 1.

Thus if x-y is negative (y is greater than x) then x - (x - y) = y.

If x-y is positive then x is greater than y, the most significant bit is zero and thus x - 0 = x.

The 31 represents the total # of bits of the variable - 1. (thus the most significant bit).

I imagine this is what they're looking for since it doesn't use comparisons.

paiego
  • 3,619
  • 34
  • 43
  • Under C++ 5.8/3, for `E1 >> E2`, "If E1 has a signed type and a negative value, the resulting value is implementation-defined". – Tony Delroy Sep 19 '14 at 05:49
  • @Tony : It's interesting to to think that an "implementation" would not use a 2s-Compliment to store a negative number yet such details are "under the covers", just like endian-ness. Do you happen to have a link to that documentation? – paiego Sep 19 '14 at 14:13
  • seems to be linked from [this SO question](http://stackoverflow.com/questions/7238958/iso-c-standard-draft) - cheers. – Tony Delroy Sep 19 '14 at 14:45
2

Aww, so many nice solutions. I have another one exploiting that booleans convert to zero and one:

#define CONDITION(c, t, f) (c * t + (1 - c) * f)
#define MAX(a, b) CONDITION(a > b, a, b)

Nearby, I'm deliberately ALL_UPPERCASING this evil macro machinery. I'd say this is the actual point you should have raised in an interview.

Ulrich Eckhardt
  • 16,572
  • 3
  • 28
  • 55
1

Another crazy C++11-only approach, and cheating slightly with an extra struct declaration (could use a std::array if libraries were allowed) - for whatever it's worth (not much!)...

struct Max { int n_[2]; };
#define max(x,y) (Max{(x),(y)}.n_[(x) < (y)])
Tony Delroy
  • 102,968
  • 15
  • 177
  • 252