3 ms·
Arbitrary addition, subtraction and multiplication without arithmetic signs. int add( int a, int b ) { while ( a ) { int c = a & b; b ^
by KeithMajhor 15y ago
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;
}