top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

C program without using Arithmetic operators for Math table?

+2 votes
611 views

Can Anyone write the C program without using Arthimethic operators for Math table?

posted Sep 18, 2013 by Giri Prasad

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
Is bitwise operator OK, also math tables means basic operations i.e. +, - , X and /. Please clarify.
Yes is it ok
u already got the answer

1 Answer

+2 votes

Hope below code snippet may help if you are ok with bitwise operator for addition

int xor, and, temp;
and = x & y;
xor = x ^ y; 

while(and != 0 )
{
 and <<= 1; 
 temp = xor ^ and;
 and &= xor; 
 xor = temp; 
}

For multiplying a and b add "a" "b" times

unsigned int mult(unsigned int a,unsigned int b)
{
    unsigned int counter=0;
    unsigned int mult = a;
    if(a == 0 || b == 0)
    {
    return 0;
    }

    //Optimize if any of the number is power of two then
    //Just right shift other with value of this number

    while(counter < b )
    {   
    counter = add(counter,1);
    mult = add(mult,a);
    }
    return mult;
}
answer Sep 18, 2013 by anonymous
Similar Questions
0 votes

I have a recursive function as below,

void func()
{
       if(counter>10)
              return;
       func();
}

Here, I need to come out of the function when counter reaches specific number(10 as per example).
Now, the condition is, I can't take this counter as global or static or can't pass this counter as parameter.

Any suggestion to solve this given above terms.

...