Skip to content

Instantly share code, notes, and snippets.

@thejoshwolfe
Created August 25, 2017 23:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thejoshwolfe/6df79a3c5b3e7b30ae184ced0f7eb9c0 to your computer and use it in GitHub Desktop.
Save thejoshwolfe/6df79a3c5b3e7b30ae184ced0f7eb9c0 to your computer and use it in GitHub Desktop.
given 3 ints, return the median
int median3(int a, int b, int c) {
int index =
((a < b) << 0) |
((b < c) << 1) |
((c < a) << 2);
switch (index) {
case 0: unreachable();
case 1: return c;
case 2: return a;
case 3: return b;
case 4: return b;
case 5: return a;
case 6: return c;
case 7: unreachable();
}
unreachable();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment