C 语言中不带算术运算符的乘法
是否可以使用 C 语言不使用算术运算符来将两个数字相乘?使用左移运算符,我只能将任何数字乘以 2。其他数字怎么样?
Is it possible to multiply two numbers with out using arithmetic operators using C? Using the left shift operator, I can multiply any number by 2 only. How about other numbers?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
要解决这个问题,您要做的第一件事就是弄清楚如何仅使用按位运算符进行简单的算术运算。
例如,可以使用这种技术实现加法
然后使用加法进行乘法:
如果 b 为负数,则该乘法尚不起作用,但由于这看起来像一个家庭作业问题,所以我将其作为练习你。 ;)
编辑:
虽然一旦有了加法,乘法就很直观了,但加法函数并不那么容易理解,所以我在这里解释一下。
假设您想要将两个数字 11010011 和 10101 相加。通常的方法是将它们排列起来,如下所示:
您会注意到,当您将两个二进制数相加时,如果两个数字的第 i 位都是 1,则结果位为 0,并且 i 左侧有一位进位。
这个进位就是代码中变量“c”存储的内容。
我们按位与 b 和 a 求和,只得到 a 和 b 都为 1 的位,然后将其左移 1 以获得进位位。
然后您可以查看 a 和 b 不同的位,即其中一位为 1,另一位为 0。在这种情况下,结果位将是 1,没有进位。
这就是该行存储的内容:
上面的行本质上删除了 a 和 b 均为 1 的位(现在存储在 c 中)。
现在你还有另外两个数字 b 和 c,需要将它们相加。
首先让我们看看在循环的第一次迭代之后示例的情况。
它可能还不完全明显,但 b 正在累加结果总和。通过更多次循环迭代,更多结转的位被“添加”回 b,进位再次存储在 c 中。从这个意义上说,您可以将异或运算符视为不带进位的加法运算。
这是该循环的第二次迭代:
第三次迭代:
现在 c(和 a)为 0,因此不再需要添加进位。我们退出循环并返回 b。请注意,即使继续循环,任何数字都不会改变。
To solve this problem, the first thing you want to do is figure out how to get simple arithmetic operations using just bitwise operators.
For example, addition can be achieved using this technique
Then it's a matter of doing multiplication using addition:
This multiplication doesn't yet work if b is negative, but since this looks like a homework problem, I'll leave that as an exercise for you. ;)
Edit:
While multiplication is intuitive once you have addition, the addition function is not so easy to understand, so I'll explain it here.
Let's say you want to add two numbers, 11010011 and 10101. The usual way to do it is to line them up like so:
You'll notice that when you add two binary numbers, if the ith bit from both numbers is 1, then the resulting bit is 0, and there is a carry over to a bit to the left of i.
This carry is what the variable 'c' in the code stores.
We bit wise and b and a to get only the bits where both a and b are 1, then we left shift it by 1 to get the carry bits.
Then you can look at the bits where a and b differ, i.e. one of the bits is 1 and the other bit is 0. The resulting bit in this case will be 1 with no carry.
that's what this line stores:
The line above essentially removes the bits where both a and b are 1 (now stored in c).
So now you have another two numbers b and c, that you need to add together.
First let's look at where we're at with the example after the first iteration of the loop
It might not be entirely obvious yet, but b is accumulating the resulting sum. Through more iterations of the loop, more bits that are carried over are "added" back to b, with carries stored again in c. In that sense, you can think of the xor operator as an addition operation without carry.
Here's the second iteration of that loop:
3rd iteration:
Now c (and a) is 0, so there is no more carry to add. We exit the loop and return b. Note that even if you continue the loop, none of the numbers will change.
这是可能的,请参阅此 wiki 了解方向: http://en.wikipedia.org/wiki/Binary_multiplier< /a>
It is possible, see this wiki for a direction: http://en.wikipedia.org/wiki/Binary_multiplier
是的,这是可能的。您可以使用逻辑运算符来完成此操作。毕竟,这就是当您使用算术运算符时硬件所做的全部事情。
Yes it is possible. You can do it with logical operators. After all, that's all that the hardware does when you use an arithmetic operator.