只用按位运算符的二进制执行算术运算 [英] Performing arithmetic operations in binary using only bitwise operators

查看:126
本文介绍了只用按位运算符的二进制执行算术运算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/2776211/how-can-i-multiply-and-divide-using-only-bit-shifting-and-adding\">How我可以繁殖,只使用比特移位并加入分?


我必须写功能,而无需使用任何算术运算符除循环控制执行二进制减法,乘法和除法。我只写code在Java中以前了,所以我有一个很难包装我的头解决这个问题。

与减法开始,我需要写有原型的函数

  INT bsub(INT X,int y)对

我知道我需要到y转换成二进制补码,以使其负面并将它添加到X,但我只知道如何使用的补〜运营商,加1要做到这一点,但我不能用+运营商。

该BADD功能提供了,我就能实现它在bsub如果我能想出​​如何使Ÿ负数。在code为BADD如下所示。感谢您事先的任何提示。

  INT BADD(INT X,int y)对{INT I;焦总和;
焦炭car_in = 0;
焦炭car_out;
烧焦A,B;unsigned int类型面膜= 00000001;
INT结果为0;对于(i = 0; I&LT; 32;我++){  一个=(X安培;掩模)!= 0;
  B =(Y&安培;面罩)!= 0;
  car_out = car_in&安培; (A | B)|一个和b;
  总和= A ^ B ^ car_in;  如果(和){
     结果| =口罩;
  }  如果(我!= 31){
     car_in = car_out;
  }其他{
     如果(car_in!= car_out){
        的printf(溢出发生\\ n);
     }
  }  面膜&LT;&LT; = 1;
  } 返回结果;
  }


解决方案

好吧,减去按位运算,而不 + - 运营商稍微棘手,但可以做到的。您有补的基本理念,但没有使用 + 变得有点棘手。

您可以通过先设定除了只逐位,然后使用该这样做,你可以做减法。它是用于补,所以code是这样的:

  INT BADD(INT N1,N2 INT){
    INT携带,总和;
    携带=(N&安培; N2)LT;&LT; 1; //查找用于进位
    总和= N1 N2 ^; //添加的每一位,丢弃随身携带。
    如果(总和购自运)//如果位匹配,加上目前的总和和携带。
        返回BADD(总和,随身携带);
    其他
        返回总和^矣; //返回的总和。
}INT bsub(INT N1,N2 INT){
    //添加补和回报。
    返回BADD(N1,BADD(〜n2时,1));
}

然后,如果我们用上面的code的一个例子:

  INT的main(){
的printf(%d个\\ N,bsub(53,17));
返回0;
}

这结束返回 36 。这就是减法是如何工作的按位只操作。

随后乘除变得更加复杂,但可以做;对于这两个操作,可使用带有变化的加法和/或减法一起把工作做好。您可能还需要阅读<一href=\"http://stackoverflow.com/questions/2776211/how-can-i-multiply-and-divide-with-only-using-bit-shifting-and-adding\">this问题和如何做这篇文章

Possible Duplicate:
How can I multiply and divide using only bit shifting and adding?

I have to write functions to perform binary subtraction, multiplication, and division without using any arithmetic operators except for loop control. I've only written code in Java before now, so I'm having a hard time wrapping my head around this.

Starting with subtraction, I need to write a function with prototype

int bsub(int x, int y)

I know I need to convert y to two's complement in order to make it negative and add it to x, but I only know how to do this by using one's complement ~ operator and adding 1, but I can't use the + operator.

The badd function was provided, and I will be able to implement it in bsub if I can figure out how to make y a negative number. The code for badd is shown below. Thanks in advance for any tips.

int badd(int x,int y){

int i;

char sum;
char car_in=0;
char car_out;
char a,b;

unsigned int mask=0x00000001;
int result=0;

for(i=0;i<32;i++){

  a=(x&mask)!=0;
  b=(y&mask)!=0;
  car_out=car_in & (a|b) |a&b;
  sum=a^b^car_in;

  if(sum) {
     result|=mask;
  }

  if(i!=31) {
     car_in=car_out;
  } else {
     if(car_in!=car_out) {
        printf("Overflow occurred\n");
     }
  }

  mask<<=1;
  }

 return result;
  }

解决方案

Well, subtracting in bitwise operations without the + or - operators is slightly tricky, but can be done. You have the basic idea with the complement, but without using + it becomes slightly tricky.

You can do it by first setting up addition with bit-wise only, then using that, you can do subtraction. Which is used for the complement, So the code looks like this:

int badd(int n1, int n2){
    int carry, sum;
    carry = (n1 & n2) << 1; // Find bits that are used for carry
    sum = n1 ^ n2; // Add each bit, discard carry.
    if (sum & carry) // If bits match, add current sum and carry.
        return badd(sum, carry);
    else
        return sum ^ carry; // Return the sum.
}

int bsub(int n1, int n2){
    // Add two's complement and return.
    return badd(n1, badd(~n2, 1));
}

And then if we use the above code in an example:

int main(){
printf("%d\n", bsub(53, 17));
return 0;
}

Which ends up returning 36. And that is how subtraction works with bitwise only operations.

Afterwards multiplication and division get more complicated, but can be done; for those two operations, use shifts along with addition and/or subtraction to get the job done. You may also want to read this question and this article on how to do it.

这篇关于只用按位运算符的二进制执行算术运算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆