使用按位运算符乘以两个整数

Sup*_*Man 25 bitwise-operators

如何使用按位运算符乘以两个整数?

我在这里找到了一个实现.有没有更好的实现乘法的方法?

例如:必须使用按位运算符执行2*6 = 12.

注意:数字是任意的,不是2的幂

zen*_*ngr 42

#include<stdio.h>

main()
{
    int a, b, result;
    printf("\nEnter the numbers to be multiplied:");
    scanf("%d%d", &a, &b);       // a > b
    result = 0;
    while (b != 0)               // Iterate the loop till b == 0
    {
        if (b & 01)               // Bitwise & of the value of b with 01
        {
            result = result + a;  // Add a to result if b is odd .
        }
        a<<=1;                    // Left shifting the value contained in 'a' by 1
                                  // Multiplies a by 2 for each loop
        b>>=1;                    // Right shifting the value contained in 'b' by 1.
    }
    printf("nResult:%d",result);
}
Run Code Online (Sandbox Code Playgroud)

资源


Shi*_*hiv 11

我来到这里寻找这个问题,我发现Zengr的答案是正确的.谢谢Zengr!但是有一个修改我想看到哪个修改在他的代码中摆脱了'+'运算符.这应该使用NO算术运算符乘以两个任意数字,但都是按位的.

Zengr的解决方案首先:

#include<stdio.h>
main()
{
   int a,b,result;     
   printf("nEnter the numbers to be multiplied :");
   scanf("%d%d",&a,&b);         // a>b
   result=0;
   while(b != 0)               // Iterate the loop till b==0
   {
        if (b&01)                // Bitwise &  of the value of b with 01
        {
          result=result+a;     // Add a to result if b is odd .
        }
        a<<=1;                   // Left shifting the value contained in 'a' by 1 
                           // multiplies a by 2 for each loop
        b>>=1;                   // Right shifting the value contained in 'b' by 1.
   }
   printf("nResult:%d",result);
}
Run Code Online (Sandbox Code Playgroud)

我的答案是:

#include<stdio.h>
main()
{
   int a,b,result;     
   printf("nEnter the numbers to be multiplied :");
   scanf("%d%d",&a,&b);         // a>b
   result=0;
   while(b != 0)               // Iterate the loop till b==0
   {
        if (b&01)                // Bitwise &  of the value of b with 01
        {
          result=add(result,a);     // Add a to result if b is odd .
        }
        a<<=1;                   // Left shifting the value contained in 'a' by 1 
                                // multiplies a by 2 for each loop
        b>>=1;                   // Right shifting the value contained in 'b' by 1.
   }
   printf("nResult:%d",result);
}
Run Code Online (Sandbox Code Playgroud)

在哪里我会写add()为:

int Add(int x, int y)
{
    // Iterate till there is no carry  
    while (y != 0)
    {
        // carry now contains common set bits of x and y
        int carry = x & y;  

        // Sum of bits of x and y where at least one of the bits is not set
        x = x ^ y; 

        // Carry is shifted by one so that adding it to x gives the required sum
        y = carry << 1;
    }
    return x;
}
Run Code Online (Sandbox Code Playgroud)

或递归添加为:

int Add(int x, int y)
{
    if (y == 0)
        return x;
    else
        return Add( x ^ y, (x & y) << 1);
}
Run Code Online (Sandbox Code Playgroud)

添加代码的来源:http://www.geeksforgeeks.org/add-two-numbers-without-using-arithmetic-operators/


art*_*hek 6

这个纯粹是按位操作.

public int bitwiseMultiply(int a, int b) {
    if (a ==0  || b == 0) {
        return 0;
    }

    if (a == 1) {
        return b;
    }
    else
        if (b == 1) {
            return a;
        }


    int result = 0; // Not needed, just for test
    int initA = a;
    boolean isORNeeded = false;
    while (b != 0 ) {

        if (b == 1) {
            break;
        }

        if ((b & 1) == 1) { // Carry needed, odd number
            result += initA; // Test, not needed
            isORNeeded = true;
        }

        a <<= 1; // Double the a
        b >>= 1; // Half the b
        System.out.println("a=["+a+"], b=["+b+"], result=["+result+"]");
    }

    return (isORNeeded ? (a | initA) : a); // a + result;
}
Run Code Online (Sandbox Code Playgroud)