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);
}
4
  • See this answer and this c++ code will not compile in C++ compiler.main should return int. Commented Sep 19, 2014 at 4: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? Commented Sep 19, 2014 at 8: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++? Commented Sep 19, 2014 at 8: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. Commented Sep 22, 2014 at 7:07

6 Answers 6

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)))
Sign up to request clarification or add additional context in comments.

1 Comment

And another +1 for using parens they way they should be used in a macro.
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

3 Comments

Nice because it only assumes the types are ordered, no arithmetic assumed. I.e. it also works on forward iterators.
It actually looks like some fake ternary operator. But I doubt this compiles.
@MichaelWalz: it's a "lambda" and only available in recent C++11 Standards.
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.

2 Comments

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.
abs() would be a standard library function.
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.

3 Comments

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 : 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?
seems to be linked from this SO question - cheers.
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.

Comments

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)])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.