问题描述
我如何使用位运算符(不只是2的幂部门)落实分工?
How can I implement division using bit-wise operators (not just division by powers of 2)?
详细描述它。
推荐答案
做除法的标准方法是通过实施二进制长除法。这涉及到减法,所以只要你不打折这是不是一个位操作,那么这就是你应该做的。 (请注意,您当然可以实现减法,很繁琐,使用按位逻辑运算。)
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.)
在本质上,如果你正在做 Q = N / D
:
In essence, if you're doing Q = N/D
:
- 对齐的
N
和D
的最显著的。 - 计算
T =(N - D);
。 - 如果
(T> = 0)
,然后设置问:
的最低显著位为1,设置和n = t
。 - 左移
N
1 - 左移
问:
1 - 转到步骤2。
- Align the most-significant ones of
N
andD
. - Compute
t = (N - D);
. - If
(t >= 0)
, then set the least significant bit ofQ
to 1, and setN = t
. - Left-shift
N
by 1. - Left-shift
Q
by 1. - Go to step 2.
环路尽可能多输出位(包括小数),根据您的需要,再申请一个最终的转变撤消在步骤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.
这篇关于实施司与逐位运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!