How to perform multiplication, using bitwise operators?

前端 未结 7 1826
一个人的身影
一个人的身影 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条回答
  • 2020-12-04 13:56

    To multiply two binary encoded numbers without a multiply instruction. It would be simple to iteratively add to reach the product.

    unsigned int mult(x, y)
    unsigned int x, y;
    {
        unsigned int reg = 0;
    
        while(y--)
            reg += x;
        return reg;
    }
    

    Using bit operations, the characteristic of the data encoding can be exploited. As explained previously, a bit shift is the same as multiply by two. Using this an adder can be used on the powers of two.

    // multiply two numbers with bit operations
    
    unsigned int mult(x, y)
    unsigned int x, y;
    {
        unsigned int reg = 0;
    
        while (y != 0)
        {
            if (y & 1)
            {
                reg += x;
            }
            x <<= 1;
            y >>= 1;
        }
        return reg;
    }
    
    0 讨论(0)
  • 2020-12-04 14:03
    -(int)multiplyNumber:(int)num1 withNumber:(int)num2
    {
        int mulResult =0;
        int ithBit;
    
        BOOL isNegativeSign = (num1<0 && num2>0) || (num1>0 && num2<0)   ;
        num1 = abs(num1);
        num2 = abs(num2);
    
    
        for(int i=0;i<sizeof(num2)*8;i++)
        {
            ithBit =  num2 & (1<<i);
            if(ithBit>0){
                mulResult +=(num1<<i);
            }
    
        }
    
        if (isNegativeSign) {
            mulResult =  ((~mulResult)+1 );
        }
    
        return mulResult;
    }
    
    0 讨论(0)
  • 2020-12-04 14:05

    To multiply by any value of 2 to the power of N (i.e. 2^N) shift the bits N times to the left.

    0000 0001 = 1 
    
    times 4 = (2^2 => N = 2) = 2 bit shift : 0000 0100 = 4
    
    times 8 = (2^3 -> N = 3) = 3 bit shift : 0010 0000 = 32
    

    etc..

    To divide shift the bits to the right.

    The bits are whole 1 or 0 - you can't shift by a part of a bit thus if the number you're multiplying by is does not factor a whole value of N ie.

    since: 17 = 16  + 1 
    thus:  17 = 2^4 + 1
    
    therefore: x * 17 = (x * 16) + x in other words 17 x's  
    

    thus to multiply by 17 you have to do a 4 bit shift to the left, and then add the original number again:

    ==> x * 17 = (x * 2^4) + x 
    ==> x * 17 = (x shifted to left by 4 bits) + x 
    
    so let x = 3 = 0000 0011 
    
    times 16 = (2^4 => N = 4) = 4 bit shift : 0011 0000 = 48
    
    plus the x (0000 0011)
    

    ie.

        0011 0000  (48)  
    +   0000 0011   (3)
    =============
        0011 0011  (51)
    

    Edit: Update to the original answer. Charles Petzold has written a fantastic book 'Code' that will explain all of this and more in the easiest of ways. I thoroughly recommend this.

    0 讨论(0)
  • 2020-12-04 14:07
    public static int multi(int x, int y){
            boolean neg = false;
            if(x < 0 && y >= 0){
                x = -x;
                neg = true;
            }
            else if(y < 0 && x >= 0){
                y = -y;
                neg = true;
            }else if( x < 0 && y < 0){
                x = -x;
                y = -y;
            }
    
            int res = 0;
            while(y!=0){
                if((y & 1) == 1) res += x;
                x <<= 1;
                y >>= 1;
            }
            return neg ? (-res) : res;
        }
    
    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
  • 2020-12-04 14:18

    You'd factor the multiplicand into powers of 2.
    3*17 = 3*(16+1) = 3*16 + 3*1 ... = 0011b << 4 + 0011b

    0 讨论(0)
提交回复
热议问题