Hi ,
I have plan to write a programs for adition,subtraction , division and multiplication between two operands by using bitwise operators with out +,-,*and /.
Is there any systematic way to create these logics?
I looked at one example for multiplication as below by googled. by seeing that , i need to "byheart" :-) that logic to remember.
without by heart is there any easy systematic way to do all those things?
Code:
#include<stdio.h>
int main()
{
int a,b,result;
printf("nEnter the numbers to be multiplied :");
scanf("%d%d",&a,&b);
printf ("\n");
result=0;
while(b != 0) // Iterate the loop till b==0
{
rintf ("value of a=%d b=%d \n",a,b);
if (b&01) // Logical ANDing of the value of b with 01
{
printf ("value of a=%d b=%d \n",a,b);
result=result+a; // Update the result with the new value of a.
printf ("result=%d\n",result);
}
a<<=1; // Left shifting the value contained in 'a' by 1.
b>>=1; // Right shifting the value contained in 'b' by 1.
}
printf("nResult:%d\n",result);
return 0;
}
Please let me know any suggestion