How to multiply of two numbers with shift operator in C

1 Answer

0 votes
#include <stdio.h>
  
int multiply(int a, int b)
{  
    int mul = 0, count = 0;
    while (b) {
        if (b % 2 == 1)              
            mul += a << count;
        count++;
        b /= 2;
    }
    return mul;
}
 
int main()
{
    int a = 3 , b = 7;
    
    printf("%d\n", multiply(a, b));
    
    return 0;
}
   
/*
  
run:
  
21
  
*/

 



answered Jul 21, 2018 by avibootz
...