News for hackers. Stuff that matters.
Re:Calculate n + 1 without using + or - or * or / by KeithMajhor on Saturday July 02, 2011 @03:02AM (#2721211)
Arbitrary addition, subtraction and multiplication without arithmetic signs.

int add( int a, int b ) { while ( a ) { int c = a & b; b ^= a; a = c << 1; } return b; }

int sub( int a, int b ) { return add( a, add( ~b, 1 ) ); }

int mul( int a, int b ) { int c = 0; while ( a ) { if ( a & 1 ) c = add( c, b ); a >>= 1; b <<= 1; } return c; }

There are no comments on this comment yet.