Python 3.x 解释移位加法器乘法
有人能用虚拟术语向我解释二进制值乘法的移位加法器方法吗?我不理解,更不用说写一个程序来计算答案了。我想你知道标准的纸笔乘法方法(aka) 二进制移位加和长乘法方法非常相似 例如,在长乘法方法中,如果要将二进制数1001乘以1011,程序如下所示:Python 3.x 解释移位加法器乘法,python-3.x,binary,Python 3.x,Binary,有人能用虚拟术语向我解释二进制值乘法的移位加法器方法吗?我不理解,更不用说写一个程序来计算答案了。我想你知道标准的纸笔乘法方法(aka) 二进制移位加和长乘法方法非常相似 例如,在长乘法方法中,如果要将二进制数1001乘以1011,程序如下所示: 1 0 0 0 x 1 0 1 1 --------------- 1 0 0 0 ( Step 1: 1000 x LSB (bit 0) of 1011, which is 1, follow
1 0 0 0
x 1 0 1 1
---------------
1 0 0 0 ( Step 1: 1000 x LSB (bit 0) of 1011, which is 1, followed by 0 shift to the left)
+ 1 0 0 0 - ( Step 2: 1000 x bit 1 of 1011, which is again 1, followed by 1 shift to the left)
+ 0 0 0 0 - - ( Step 3: 1000 x bit 2 of 1011, which is 0, followed by 2 shifts to the left)
+ 1 0 0 0 - - - ( Step 4: 1000 x MSB (bit 3) of 1011 which is 1 again, followed by 3 shifts to the left)
----------------
1 0 1 1 0 0 0 ( Step 5: Add all the above)
----------------
现在,在shift和add方法中,您最终不会进行加法(步骤5),而是继续动态添加数字,如下所示:
Step 0: Result = 0
Step 1: Result = Result + Step 1 of Long division (1000 x LSB (bit 0) of 1011, which is 1, followed by 0 shift)
= 0000 + 1000
= 1000
Step 2: Result = Result + Step 2 of Long division (1000 x bit 1 of 1011, which is again 1, followed by 1 shift)
= 1000 + 10000 (Additional zero at the end of second term is due to shifting 1000 to the left by 1 time: 1000_0)
= 11000
Step 3: Result = Result + Step 3 of Long division (1000 x bit 2 of 1011, which is 0, followed by 2 shift)
= 11000 + 000000
= 11000
Step 4: Result = Result + Step 4 of Long division (1000 x MSB (bit 3) of 1011 which is 1 again, followed by 3 shift)
= 11000 + 1000000 (Additional zeros at the end of second term is due to shifting 1000 to the left three times: 1000_000)
= 1011000
我希望以上算法足以让您开始编码。谢谢您的帮助。这种方法对计算机更有效吗?因为存储计算值所需的内存更少(结果不断变化)。虽然与许多其他算法相比,这可能是一种内存效率高的方法,但就内存效率而言,我不确定这是否是最好的方法。这种方法的一个缺点是为了提高内存效率而牺牲时钟周期。如果你看长乘法,我们可以在两个时钟周期内得到最终结果:步骤1、2、3、4可以并行完成,每个都需要一个时钟周期。在下一个时钟周期中,将上述(步骤5)的所有结果相加,得到最终结果。另一方面,移位加法器需要4个时钟周期才能得到最终结果