Skip to content

Instantly share code, notes, and snippets.

@vstakhov
Created February 1, 2014 14:15
Show Gist options
  • Select an option

  • Save vstakhov/8752923 to your computer and use it in GitHub Desktop.

Select an option

Save vstakhov/8752923 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <limits.h>
int f(int x)
{
int y = x >> 31;
return (x ^ y) - y;
}
void abs_test(int in)
{
printf("in=%d, abs=%d\n", in, f(in));
}
int main (int argc, char **argv)
{
int x = -45, x0 = 100500, x1 = INT_MIN, x2 = 0, x3 = INT_MAX;
abs_test(x);
abs_test(x0);
abs_test(x1);
abs_test(x2);
abs_test(x3);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment