0

I'm implementing a simple (virtual) ALU and some other chips (adder, multiplier etc.).

I'm using the 2's complement representation for my numbers.

For multiplication of x and y, two 16-bit numbers, I thought I'd use left shifts along these lines (this is of course performed without an actual loop):

  • set sum[0..15]=0

  • set x'=x

  • for i=0...15 //(y[0] is LSB and y[15] is MSB)

    • add x' to sum if y[i]=1 and shift x' left.

(Is this the standard way?)

My problem is with the left shifts:

If there's i s.t. x[i]=1, at some point the MSB of x' will become 1, and that negates it.

That's a problem since for example 2*2 using the method above gives "-4".

So, my actual question is: when shifting left do I also need to take into consideration the sign bit?

pnuts
  • 58,317
  • 11
  • 87
  • 139
Paz
  • 737
  • 7
  • 22

1 Answers1

0

Yes, you do.

A simple approach could be to save the sign somewhere, convert to unsigned, perform the math, then convert back if it was negative.

  • So shifting left 0110 0000 0000 0000 is supposed to produce: 0100 0000 0000 0000? and, is it not enough to save the sign, shift and then replace to current sign bit with the original one? – Paz Feb 21 '14 at 11:12
  • This is an overflow condition, but yes. -- No, you have to convert the sign with NEG, or similar. In two's complement, -1 is represented as all ones, and so on. – 500 - Internal Server Error Feb 21 '14 at 11:25