Multiply using shift, for signed numbers

Discussion in 'VHDL' started by fastgreen2000@yahoo.com, Sep 9, 2005.

  1. Guest

    I'm trying to achieve multiply using one or more shift operations. For
    example,
    A = 5*B can be written as
    A = 4*B + B or
    A = (B sll 2) + B

    Now, B is a signed number, so when I do (B sll 2), the shift operation
    doesn't discriminate the sign bit, and I end up losing it (falls off to
    the left side).

    One solution would be to save the MSb of B, perform 'sll' on the rest,
    and concatenate them together. If B were signed(7 downto 0) :
    A = ((B(7) & (B(6 downto 0) sll 2)) + B;

    Is there a better way to do this, perhaps some built-in function I
    couldn't find?

    I'm thinking that there has to be an easier way. I'm not concerned
    about the overflow (yet). Can anyone help? I'm not that fluent in
    VHDL. Thank you.
    , Sep 9, 2005
    #1
    1. Advertising

  2. Hubble Guest

    I think you misunderstood something. signed is two's complement, so
    using 8 bits you have

    2: 00000010
    1: 00000001
    0: 00000000
    -1: 11111111
    -2: 11111110
    -3: 11111101
    -4: 11111100

    if you shift left, sign bits won't fall off.

    Hubble.
    Hubble, Sep 12, 2005
    #2
    1. Advertising

  3. Hubble Guest

    Having thought a bit on this topic, the sll operator does not multiply
    by 2**n on 2s complement negative numbers. Since you want to use Logic
    Half- and fulladdres, 2s complement should be the representation of
    choice. (Note: If you use a sign bit and a mantissa, you have to
    redefine the +/- operations as well).

    So you have to convert a negative number to a positive, shift, add and
    invert it again, like this (without test)

    variable highbit: std_logic;
    ...
    highbit:=B(B'high);
    if highbit='1' then
    B:=abs(B);
    end if;
    Btimes5:=B ssl 4 + B;
    if highbit='1' then
    Btimes5:=-Btimes5;
    end if;

    Hubble.
    Hubble, Sep 13, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roberto Gallo

    Shift - byte[] buf shift

    Roberto Gallo, Jan 27, 2004, in forum: Java
    Replies:
    3
    Views:
    2,025
    Thomas Schodt
    Jan 27, 2004
  2. Wenjie
    Replies:
    3
    Views:
    1,019
    Ron Samuel Klatchko
    Jul 11, 2003
  3. Santosh Nayak

    Left Shift / Right Shift Operators

    Santosh Nayak, Nov 30, 2006, in forum: C Programming
    Replies:
    16
    Views:
    1,433
    CBFalconer
    Nov 30, 2006
  4. Sanny
    Replies:
    38
    Views:
    3,348
    Thomas Richter
    Apr 29, 2011
  5. devphylosoff

    what "shift" does, if not "$_ = shift;" ?

    devphylosoff, Nov 29, 2007, in forum: Perl Misc
    Replies:
    3
    Views:
    315
    Michele Dondi
    Dec 4, 2007
Loading...

Share This Page