Created
August 24, 2015 01:17
-
-
Save zhangys-lucky/12329fa44686c55c34b1 to your computer and use it in GitHub Desktop.
There are two int variables: a and b, don’t use “if”, “? :”, “switch”or other judgement statements, find out the biggest one of the two numbers.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int max(int a,int b) | |
{ | |
return ( ( a + b ) + abs( a – b ) ) / 2; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int max(int a,int b) | |
{ | |
return *( &a + ((unsigned)(a - b) >> (sizeof(int) * 8 - 1)) ); | |
} | |
int min(int a,int b) | |
{ | |
return *( &b - ((unsigned)(a -b )> > ( sizeof(int) * 8 - 1)) ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment