Skip to content

Instantly share code, notes, and snippets.

@zhangys-lucky
Created August 24, 2015 01:17
Show Gist options
  • Save zhangys-lucky/12329fa44686c55c34b1 to your computer and use it in GitHub Desktop.
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.
int max(int a,int b)
{
return ( ( a + b ) + abs( a – b ) ) / 2;
}
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