Question:
--------------
Two integers a, b how to divde a/b without using /,% operator... (repeted subtraction is not the solution).
Solution:
-----------
#include<stdio.h>
int main()
{
printf("%d\n", divide(40,2));
return 0;
}
int divide(int num, int denom)
{
int a=0, b=0;
int i= 31; // CAREFUL: works only on int=32-bit machine!
/* Work from leftmost to rightmost bit in numerator */
while(i>=0) {
/* appends one bit from numerator to a */
a = (a << 1) + ((num & (1 << i)) >> i);
b = b << 1;
printf("After shifting a=%d and b=%d\n",a,b);
if (a >= denom) {
a -= denom;
b++;
}
printf("After subtraction a=%d and b=%d\n",a,b);
i--;
}
return b;
}
--------------
Two integers a, b how to divde a/b without using /,% operator... (repeted subtraction is not the solution).
Solution:
-----------
#include<stdio.h>
int main()
{
printf("%d\n", divide(40,2));
return 0;
}
int divide(int num, int denom)
{
int a=0, b=0;
int i= 31; // CAREFUL: works only on int=32-bit machine!
/* Work from leftmost to rightmost bit in numerator */
while(i>=0) {
/* appends one bit from numerator to a */
a = (a << 1) + ((num & (1 << i)) >> i);
b = b << 1;
printf("After shifting a=%d and b=%d\n",a,b);
if (a >= denom) {
a -= denom;
b++;
}
printf("After subtraction a=%d and b=%d\n",a,b);
i--;
}
return b;
}
Powered by ScribeFire.
No comments:
Post a Comment