How to perform multiplication, using bitwise operators?

前端 未结 7 1892
一个人的身影
一个人的身影 2020-12-04 13:28

I am working through a problem which i was able to solve, all but for the last piece - i am not sure how can one do multiplication using bitwise operators:

0         


        
7条回答
  •  Happy的楠姐
    2020-12-04 14:07

    I have just realized that this is the same answer as the previous one. LOL sorry.

    public static uint Multiply(uint a, uint b)
    {
       uint c = 0;
       while(b > 0)
       {
          c += ((b & 1) > 0) ? a : 0;
          a <<= 1;
          b >>= 1;
       }
       return c;
    }
    

提交回复
热议问题