怎么办整数两者相辅相成,乘法和除法? [英] how to do two complement multiplication and division of integers?
问题描述
我已阅读本帖子上使用两个二进制补码乘法。但对我来说并不很清楚。即使我很难理解维基在这篇文章。我想知道如何去计算负数的乘法:
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步:符号扩展
这两个整数的两倍位。这是安全的,但可能不总是必要的。
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
09月03日:取结果位的正确数量从结果的至少显著部
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 0010011110
take the last 8 bits i.e 10011110
让我说明你提供的例子: 1 X -7
4位再presentation
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)
您可以得到更多的细节这里;
you could get more details here;
有关划分:转换为正,计算后调整迹象。我将离开这个锻炼,但你可以参考这个页面。
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屋!