Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 922
eagertoLearn Avatar asked Dec 27 '13 01:12

eagertoLearn


People also ask

How do you do two's complement multiplication?

"No Thinking Method" for Two's Complement Multiplication In 2's complement, to always get the right answer without thinking about the problem, sign extend both integers to twice as many bits. Then take the correct number of result bits from the least significant portion of the result.

What is 2's complement with example?

To get 2's complement of binary number is 1's complement of given number plus 1 to the least significant bit (LSB). For example 2's complement of binary number 10010 is (01101) + 1 = 01110.

What are complement integers?

The ones' complement binary numeral system is characterized by the bit complement of any integer value being the arithmetic negative of the value. That is, inverting all of the bits of a number (the logical complement) produces the same result as subtracting the value from 0.

What is 2's complement representation?

Two's complement representation is a way to represent the signed numbers in a digital computer. The main goal is to develop a technique which replaces a subtraction operation with an addition. In this way, we will be able to use the same circuit to perform both addition and subtraction.


1 Answers

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

step 2: do elementary multiplication

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

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

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.

like image 197
brain storm Avatar answered Oct 04 '22 02:10

brain storm