使用按位运算符实现除法
如何使用按位运算符实现除法(不仅仅是除以 2 的幂)?
详细描述一下。
How can I implement division using bit-wise operators (not just division by powers of 2)?
Describe it in detail.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
进行除法的标准方法是实现二进制长除法。这涉及减法,因此只要您不将其视为不是按位运算,那么这就是您应该做的。 (请注意,您当然可以使用按位逻辑运算实现减法,非常繁琐。)
本质上,如果您正在执行
Q = N/D
:的最高有效位N
和D
。t = (N - D);
。(t >= 0)
,则将Q
的最低有效位设置为1,并设置N = t
。N
左移 1。Q
左移 1。根据需要循环获取尽可能多的输出位(包括小数),然后应用最后一班撤消您在步骤 1 中所做的操作。
The standard way to do division is by implementing binary long-division. This involves subtraction, so as long as you don't discount this as not a bit-wise operation, then this is what you should do. (Note that you can of course implement subtraction, very tediously, using bitwise logical operations.)
In essence, if you're doing
Q = N/D
:N
andD
.t = (N - D);
.(t >= 0)
, then set the least significant bit ofQ
to 1, and setN = t
.N
by 1.Q
by 1.Loop for as many output bits (including fractional) as you require, then apply a final shift to undo what you did in Step 1.
使用按位运算符对两个数字进行除法。
Division of two numbers using bitwise operators.
我假设我们正在讨论整数除法。
假设我有两个数字 1502 和 30,我想计算 1502/30。我们是这样做的:
首先,我们将 30 与 1501 的最高位数字对齐; 30 变成 3000。然后将 1501 与 3000 进行比较,1501 包含 3000 中的 0。然后我们将 1501 与 300 进行比较,它包含 300 中的 5,然后将 (1501-5300) 与 30 进行比较。最后我们得到 5 (10^1) = 50 作为此除法的结果。
现在将 1501 和 30 都转换为二进制数字。然后,我们不是将 30 乘以 (10^x) 以将其与 1501 对齐,而是将 2 进制的 (30) 乘以 2^n 来对齐。而2^n可以转化为左移n个位置。
这是代码:
没有测试它,但你明白了。
I assume we are discussing division of integers.
Consider that I got two number 1502 and 30, and I wanted to calculate 1502/30. This is how we do this:
First we align 30 with 1501 at its most significant figure; 30 becomes 3000. And compare 1501 with 3000, 1501 contains 0 of 3000. Then we compare 1501 with 300, it contains 5 of 300, then compare (1501-5300) with 30. At so at last we got 5(10^1) = 50 as the result of this division.
Now convert both 1501 and 30 into binary digits. Then instead of multiplying 30 with (10^x) to align it with 1501, we multiplying (30) in 2 base with 2^n to align. And 2^n can be converted into left shift n positions.
Here is the code:
Didn't test it, but you get the idea.
这个解决方案效果很好。
This solution works perfectly.
不使用除法运算符实现除法:
您将需要包括减法。但这就像你手工做的一样(仅以 2 为基础)。附加的代码提供了一个简短的函数来完成此任务。
Implement division without divison operator:
You will need to include subtraction. But then it is just like you do it by hand (only in the basis of 2). The appended code provides a short function that does exactly this.
无符号长除法 (JavaScript) - 基于维基百科文章:https://en.wikipedia.org/wiki/除法算法:
“长除法是用于以十进制表示的多位数字的笔和纸除法的标准算法。它从被除数的左端逐渐移动到右端,减去除数的最大可能倍数(在每个阶段的倍数就变成商的位数,最后的差就是余数。
当与二进制基数一起使用时,此方法构成了下面的(无符号)整数除法与余数算法的基础。“
最后的函数divideWithoutDivision将其包装以允许负操作数。我用它来解决leetcode问题“除Self之外的数组的乘积”
Unsigned Long Division (JavaScript) - based on Wikipedia article: https://en.wikipedia.org/wiki/Division_algorithm:
"Long division is the standard algorithm used for pen-and-paper division of multi-digit numbers expressed in decimal notation. It shifts gradually from the left to the right end of the dividend, subtracting the largest possible multiple of the divisor (at the digit level) at each stage; the multiples then become the digits of the quotient, and the final difference is then the remainder.
When used with a binary radix, this method forms the basis for the (unsigned) integer division with remainder algorithm below."
Function divideWithoutDivision at the end wraps it to allow negative operands. I used it to solve leetcode problem "Product of Array Except Self"
下面的方法是考虑到两个数字都是正数的二进制除法的实现。如果需要考虑减法,我们也可以使用二元运算符来实现。
代码
The below method is the implementation of binary divide considering both numbers are positive. If subtraction is a concern we can implement that as well using binary operators.
Code
对于整数:
For integers:
对于 C 的移位行为的常见警告,这应该适用于无符号数量,无论 int 的本机大小如何...
With the usual caveats about C's behaviour with shifts, this ought to work for unsigned quantities regardless of the native size of an int...
这是我仅使用按位运算实现除法的解决方案:
This is my solution to implement division with only bitwise operations:
所有这些解决方案都太长了。基本思想是将商(例如 5=101)写为 100 + 00 + 1 = 101。
All these solutions are too long. The base idea is to write the quotient (for example, 5=101) as 100 + 00 + 1 = 101.
由于按位运算适用于 0 或 1 的位,因此每个位代表 2 的幂,因此如果我有位
1010
,则该值是 10。
每个位都是 2 的幂,因此如果我们将位移至对,我们除以 2
1010 --> 0101
0101 是 5
,所以,一般来说,如果你想除以 2 的某个幂,你需要右移你提高 2 到的指数,以获得该值,
例如,要除以 16,你需要除以 4 ,如 2^^4 = 16。
Since bit wise operations work on bits that are either 0 or 1, each bit represents a power of 2, so if I have the bits
1010
that value is 10.
Each bit is a power of two, so if we shift the bits to the right, we divide by 2
1010 --> 0101
0101 is 5
so, in general if you want to divide by some power of 2, you need to shift right by the exponent you raise two to, to get that value
so for instance, to divide by 16, you would shift by 4, as 2^^4 = 16.