Skip to content

Instantly share code, notes, and snippets.

@Const-me
Last active November 12, 2016 21:14
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 Const-me/a37625a1b2fc5129ee5bb94f583aba7d to your computer and use it in GitHub Desktop.
Save Const-me/a37625a1b2fc5129ee5bb94f583aba7d to your computer and use it in GitHub Desktop.
#include "stdafx.h"
int f( int x )
{
if( 0 == x )
return 0;
if( 0x80000000 == x )
return 0x80000001;
const int c = ( ( x >> 31 ) & 1 ) * -2 + 1;
const int mul = ( x & 1 ) * 2 - 1;
return c + x * mul;
}
int main()
{
int tests[] =
{
-12, -11, 0, 7, 8
};
for( int i : tests )
{
printf( "%i\t%i\t%i\n", i, f( i ), f( f( i ) ) );
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment