执行位无师算术运算符 [英] Performing bit division without arithmetic operators

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

问题描述

我想完成需要我写三种功能为二进制算术的分配。 BADD()是对我提供的,所以我用它来帮助编写bsub()和bmult()函数。我无法理解我应该怎么然而执行BDIV()函数。我知道我需要通过使用右移和我bsubb()函数位迭代,但我不知道如何实现它。下面是我到目前为止写的功能。让我知道你是否注意到我在写他们犯任何错误(即bsub()和bmult())。谢谢。

  / **此功能增加了使用位运算符的两个参数。你的
*实现不应该使用算术运算符除循环
* 控制。整数是32位长。这个函数打印一条消息
*说:发生溢出\\ N如果发生二进制补码溢出
*在加入过程。的总和作为返回值
*该函数。
* /
INT BADD(INT X,int y)对{INT I;焦总和;
焦炭car_in = 0;
焦炭car_out;
烧焦A,B;unsigned int类型面膜= 00000001;
INT结果为0;对于(i = 0; I< 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);
     }
  }  面膜<< = 1;
} 返回结果;
 }//通过查找compliemnt subracts两个整数
Y,加1,并使用BADD的//()函数
//添加-y和x
INT bsub(INT X,int y)对{返回BADD(X,BADD(〜Y,1));
}
//添加X总额为然而,许多ÿ
INT bmult(INT X,int y)对{INT总;
INT I;
对于(i = 0; I< = Y;我++)
{
 总= BADD(总数,x)的
}
 总回报;
}//我的评论
unsigned int类型BDIV(unsigned int类型红利,无符号整型除数){//给我写信
返回0;
}


解决方案

这里就不多说了,它在只是一些基本的数学基础-2:

  unsigned int类型bmult(unsigned int类型的x,unsigned int类型Y)
{
    INT总= 0;
    INT I;    / *如果第i位为非零,添加的x到总* /
    / *多总2每一步* /
    对于(I = 32; I> = 0;我 - )
    {
        总<< = 1;
        如果((Y及(1下;&所述; i))的> I标记)
        {
            总= BADD(总的,x)的;
        }
    }    总回报;
}unsigned int类型BDIV(unsigned int类型红利,无符号整型除数)
{
    INT I,商= 0,余数= 0;    如果(除数== 0){printf的(分区零\\ n);返回0; }    对于(I = 31; I> = 0;我 - )
    {
        商数&所述;&下; = 1;
        其余与所述;&下; = 1;
        余| =(分红及(1 LT;< I))>>一世;        如果(余> =除数)
        {
            余数= bsub(余,除数);
            商| = 1;
        }
    }    返回商;
}

这两篇文章都足以code这些样本:股利德穆尔

I am trying to complete an assignment that requires me to write three functions for binary arithmetic. badd() was provided for me, so I used it to help write the bsub() and bmult() functions. I am having trouble understanding how I should perform the bdiv() function, however. I know I need to iterate through the bits using a right shift and my bsubb() function, but I don't know how to implement it. Below are the functions that I have written so far. Let me know if you notice any mistakes that I made in writing them(meaning bsub() and bmult()). Thanks.

/** This function adds the two arguments using bitwise operators. Your      
* implementation should not use arithmetic operators except for loop
* control. Integers are 32 bits long.  This function prints a message
* saying "Overflow occurred\n" if a two's complement overflow occurs
* during the addition process. The sum is returned as the value of
* the function.
*/
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;
 }

// subracts two integers by finding the compliemnt
// of "y", adding 1, and using the badd() function
// to add "-y" and "x"
int bsub(int x, int y){

return badd(x, badd(~y, 1));
}


//add x to total for however many y
int bmult(int x,int y){

int total;
int i;
for(i=0; i < = y; i++)
{
 total = badd(total,x)
}
 return total;
}

// comment me
unsigned int bdiv(unsigned int dividend, unsigned int divisor){

// write me
return 0;
}

解决方案

Not much to say here, it's just some basic math in base-2:

unsigned int bmult(unsigned int x, unsigned int y)
{
    int total = 0;
    int i;

    /* if the i-th bit is non-zero, add 'x' to total */
    /* Multiple total by 2 each step */
    for(i = 32 ; i >= 0 ; i--)
    {
        total <<= 1;
        if( (y & (1 << i)) >> i )
        {
            total = badd(total, x);
        }
    }

    return total;
}

unsigned int bdiv(unsigned int dividend, unsigned int divisor)
{
    int i, quotient = 0, remainder = 0;

    if(divisor == 0) { printf("div by zero\n"); return 0; }

    for(i = 31 ; i >= 0 ; i--)
    {
        quotient <<= 1;
        remainder <<= 1;
        remainder |= (dividend & (1 << i)) >> i;

        if(remainder >= divisor)
        {
            remainder = bsub(remainder, divisor);
            quotient |= 1;
        }
    }

    return quotient;
}

These two articles are enough to code these samples: Div and Mul.

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

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