仅使用位运算符以二进制执行算术运算 [英] Performing arithmetic operations in binary using only bitwise operators

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

问题描述

可能重复:
如何仅使用乘法和除法位移和加法?

我必须编写函数来执行二进制减法、乘法和除法,而不使用除循环控制之外的任何算术运算符.我之前只用 Java 编写过代码,所以我很难理解这一点.

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)

我知道我需要将 y 转换为二进制补码以使其为负并将其添加到 x,但我只知道如何通过使用补码 ~ 运算符并加 1 来做到这一点,但我不能使用+ 运算符.

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.

提供了 badd 函数,如果我能弄清楚如何使 y 为负数,我将能够在 bsub 中实现它.badd 的代码如下所示.提前感谢您的任何提示.

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
");
     }
  }

  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
", bsub(53, 17));
return 0;
}

最终返回 36.这就是减法仅用于按位运算的方式.

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天全站免登陆