Integer division without using the / or * operator

后端 未结 7 516
梦如初夏
梦如初夏 2021-01-14 15:34

I am going through an algorithms and datastructures textbook and came accross this question:

1-28. Write a function to perform integer division withou

7条回答
  •  执笔经年
    2021-01-14 16:09

    unsigned bitdiv (unsigned a, unsigned d)
    {
    unsigned res,c;
    
    for (c=d; c <= a; c <<=1) {;}
    
    for (res=0;(c>>=1) >= d; ) {
            res <<= 1;
            if ( a >= c) { res++; a -= c; }
            }
    return res;
    }
    

提交回复
热议问题