VHDL code for 2's complement

Discussion in 'VHDL' started by nick, Jul 28, 2003.

  1. nick

    nick Guest

    I am doing a project for a class and have virtually no VHDL
    experience. I need help writing some code that will subtract 2 8-bit
    numbers by using either 2's complement or 1's complement. Any help or
    guidance would be greatly appreciated. Thanks
    nick, Jul 28, 2003
    1. Advertisements

  2. ones_complement <= not(subtrahend);
    twos_complement <= not(subtrahend) + 1;

    I leave the practical application of this up to the student :).


    Marc Guardiani

    To reply directly to me, use the address given below. The domain name is
    Marc Guardiani, Jul 29, 2003
    1. Advertisements

  3. Nick,
    as someone else has pointed out, this isn't really a VHDL problem.
    First you need to be sure you understand how 1s complement and
    2s complement notations work, and how you would do a subtract.
    Then you can start to worry about how to code it in VHDL,
    which isn't hard. I'm guessing, from your lack of experience,
    that this is a fairly elementary class and you are probably
    expected to create most of the details of the hardware design,
    so simply using the VHDL library subtract function is unlikely
    to impress your prof :)

    Here's the deal. You understand how binary numbers work, yes?
    So let's play with some 4-bit binary numbers...

    five 0101
    three 0011
    two 0010

    But just for grins, let's try something else...

    five 0101
    thirteen 1101
    two 0010 whoops, I threw away the carry-16

    Is this a useful insight? Yes, because we already know how
    to make an adder (you do, don't you?!) so if we can turn
    3 into 13 in some simple way, we don't need to create
    special hardware to make a subtracter. Of course, the
    fake-subtraction I did above works because:
    a) 13 = 16-3
    b) I threw away the carry-16 out from the addition

    So we can subtract using just an adder, if only we
    can work out how to do the "16-n" calculation. It
    turns out that this is really easy to do in binary,
    if you rewrite it as "(15-n)+1"...

    fifteen 1111
    three 0011
    1100 Sheesh, all I had to do was invert every bit!

    And the +1 usually comes for free, by utilising the carry-in
    of the main adder. You don't need to do it at the same
    time as the "15-n" operation.

    So, to subtract Y = A - B, simply:

    a) Ensure that A and B have the same number of bits
    b) Invert every bit of B, to make ~B
    c) Use your adder, with its carry-in active, to compute
    Y = A + ~B + 1
    d) Throw away the most significant (carry-out) bit of the result

    And if this, together with your textbooks and class notes,
    isn't a big enough hint, then....

    Oh, the VHDL? Just a few component instances, I think.
    Or a few concurrent assignments. I don't think I could
    give you a hint without giving you most of the answer!

    Jonathan Bromley, Consultant and ex-lecturer

    DOULOS - Developing Design Know-how
    VHDL * Verilog * SystemC * Perl * Tcl/Tk * Verification * Project Services

    Doulos Ltd. Church Hatch, 22 Market Place, Ringwood, Hampshire, BH24 1AW, UK
    Tel: +44 (0)1425 471223 mail:
    Fax: +44 (0)1425 471573 Web: http://www.doulos.com

    The contents of this message may contain personal views which
    are not the views of Doulos Ltd., unless specifically stated.
    Jonathan Bromley, Jul 29, 2003
  4. nick


    Sep 12, 2012
    Likes Received:
    VHDL 2's Complement Code

    library ieee;
    use ieee.std_logic_1164.all

    entity comp2s is
    port( din : in std_logic_vector(7 downto 0);
    dout : out std_logic_vector(7 downto 0));
    end comp2s;

    architecture func of comp2s is


    dout <= not(din) + "00000001";

    end func;
    vsharma030, Sep 12, 2012
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.