如何进行整数的两个补码乘法和除法? [英] how to do two complement multiplication and division of integers?

查看:35
本文介绍了如何进行整数的两个补码乘法和除法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读这篇关于使用两个补码进行二进制乘法的帖子.但对我来说不是很清楚.甚至我也很难理解关于此的 wiki 文章.我想知道如何计算负数的乘法:

I have read this post on binary multiplication using two complement. but it is not very clear to me. Even I have difficulty understanding the wiki article on this. I want to know how to go about calculating multiplications of negative numbers:

eg: -1 with -7 should give 7.
A 4-bit, 2's complement of -1 is : 1111
A 4-bit, 2's complement of -7 is : 1001

一些逐步计算乘法的方法会有所帮助.我没有看到关于分裂的文章.如何解决这个问题?

some step-wise way of calculating the multiplication will be helpful. No article I came across talks about division. How to approach this?

推荐答案

第 1 步:sign extend 两个整数到两倍的位数.这样做是安全的,但可能并不总是必要的.

step 1: sign extend both integers to twice as many bits. This is safe to do, though may not always be necessary.

for 4-bit --> 1111, you would extend as 1111 1111
for 4-bit --> 0111,you would extend as 0000 0111

第 2 步:进行初等乘法

step 2: do elementary multiplication

sep 3:从结果的最低有效部分中获取正确数量的结果位.

sep 3: take the correct number of result bits from the least significant portion of the result.

例如:在乘法之后,你最终得到诸如 0010011110取最后 8 位,即 10011110

eg: after multiplication, you end up with something such as 0010011110take the last 8 bits i.e 10011110

让我用你提供的例子来说明:-1 X -7 in 4-bit representation

Let me illustrate with the example you provided: -1 X -7 in 4-bit representation

         1111 1111        -1
       x 1111 1001     x  -7
      ----------------    ------
          11111111         7
         00000000
        00000000
       11111111
      11111111
     11111111
    11111111
   11111111
   ----------------
1  00000000111       --->  7 (notice the Most significant bit is zer``o)
      --------  (last 8-bits needed) 

您可以了解更多详情这里;

除法:转换为正数,计算后调整符号.我将把它作为练习,但你可以参考这个 页面.

for division: convert to positive and after the calculation adjust the sign. I will leave this as exercise but you could refer this page.

这篇关于如何进行整数的两个补码乘法和除法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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