如何将两个二进制数相乘而不用“ *“运营商? [英] How to multiply two binary numbers without " * " operator?

查看:178
本文介绍了如何将两个二进制数相乘而不用“ *“运营商?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在没有  * <的情况下将两个二进制数相乘span class =code-keyword> operator ?在编程中





我尝试过:



我为整数值写这个程序。



 #include< stdio.h> 
/ *函数乘以两个数字x和y * /
int multiply(int x,int y)
{
/ * 0乘以任何值得0 * /
if(y == 0)
返回0;

/ *逐个添加x * /
if(y> 0)
return(x + multiply(x,y-1));

/ * y为负的情况* /
if(y <0)
return -multiply(x,-y);
}

int main()
{
printf(\ n%d,multiply(5,-11));
getchar();
返回0;
}

解决方案

这完全取决于你的家庭作业问题:如果你应该写一个乘以函数,它不使用乘法运算符,然后有几种方法可以解决这个问题:

1)蛮力和无知。 x * y与向自身添加x次y相同:

 x * 0 = 0 
x * 1 = x
x * 2 = x + x
x * 3 = x + x + x
...

因此找到两个数字中最小的一个,并循环多次,将另一个加到总数中。

2)更精细一点。还记得对数吗? log n (x * y)= log n (x)+ log n (y)

所以你可以使用C内置的数学函数来记录 10 x和y,然后添加然后反对它们以获得结果。

3)更精细!使用位移并添加循环以进行乘法运算。整数是二进制数,因此您可以将每个位视为乘数:乘以和除以二进制数|乘法和除法 [ ^ ] - ANDing a数字1表示最低有效位是零还是一,两个移位运算符<<和>>乘以除以2。



但是......这是你的作业,所以我不给你任何代码!


对不起,迟到的回答。

当你学会在小学进行乘法运算时,你应该学习这种有效的算法:

 456 
* 123
-----
1368
912.
456 ..
-----
46088



它使用乘以10的技巧很简单,你只需移位数字并在右边加零。

它恰好发生在这算法可用于二进制乘法,因为处理器知道如何对二进制值进行移位。

所有你需要的是bitwize运算符: C中的按位操作 - 维基百科 [ ^ ]

乘以-1:0-x 
乘以2:x<< 1
除以2:x>> 1
检查单位是0还是1:x& 1


How to multiply two binary numbers without " * " operator? In c rrogramming



What I have tried:

I write this program for integer values.

#include<stdio.h>
/* function to multiply two numbers x and y*/
int multiply(int x, int y)
{
   /* 0  multiplied with anything gives 0 */
   if(y == 0)
     return 0;
 
   /* Add x one by one */
   if(y > 0 )
     return (x + multiply(x, y-1));
  
  /* the case where y is negative */
   if(y < 0 )
     return -multiply(x, -y);
}
 
int main()
{
  printf("\n %d", multiply(5, -11));
  getchar();
  return 0;
}

解决方案

It depends exactly what your homework question is asking for: if you are supposed to write a multiply function which doesn;t use the multiply operator, then there are a couple of ways you could approach this:
1) Brute force and ignorance. x * y is the same as adding x to itself y times:

x * 0 = 0
x * 1 = x
x * 2 = x + x
x * 3 = x + x + x
...

So find the smallest of the two numbers, and loop that many times, adding the other to a total.
2) A bit more finesse. Remember logarithms? logn(x * y) = logn(x) + logn(y)
So you could use the C built in math functions to log10 x and y, add then and then anti log them to get the result.
3) A lot more finesse! Use bit shift and add in a loop to multiply. An integer is a binary number, so you can treat each bit as a multiplier: Multiplying and Dividing Binary Numbers | Multiplication and Division[^] - ANDing a number with 1 tells you if the least significant bit is zero or one, and the two shift operators << and >> multiply and divide by two.

But ... this is your homework, so I'll give you no code!


Sorry for late answer.
When you learned to do multiplications in primary school, you should have learned this efficient algorithm:

  456
 *123
-----
 1368
 912.
456..
-----
46088


it use the trick that multiplying by 10 is easy, you just shift digits and add a zero on right.
It just happen that this algorithm is usable for binary multiplication because processors know how to do the shift on binary values.
All you need is bitwize operators: Bitwise operations in C - Wikipedia[^]

multiply by -1: 0-x
multiply by  2: x << 1
divide by 2: x >> 1
Check if unit is 0 or 1: x & 1


这篇关于如何将两个二进制数相乘而不用“ *“运营商?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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