实施司与逐位运算符 [英] Implement division with bit-wise operator

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

问题描述

我如何使用位运算符(不只是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:


  1. 对齐的 N D 的最显著的。

  2. 计算 T =(N - D);

  3. 如果(T> = 0),然后设置问:的最低显著位为1,设置和n = t

  4. 左移 N 1

  5. 左移问: 1

  6. 转到步骤2。

  1. Align the most-significant ones of N and D.
  2. Compute t = (N - D);.
  3. If (t >= 0), then set the least significant bit of Q to 1, and set N = t.
  4. Left-shift N by 1.
  5. Left-shift Q by 1.
  6. 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.

这篇关于实施司与逐位运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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