Max of two numbers

Write a method which finds the maximum of two numbers. You should not use if-else or any other comparison operator.

C++:
01 int signOfnumber(const int x)
02 {
03     return !((x>>31)&0x1);
04 }
05 
06 int maxOftwo(int x, int y)
07 {
08     int z=xy;
09     int p=signOfnumber(z);
10     int q=p^0x1;
11     return p*x+q*y;
12 }
Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s