0

I have to transfer a binary number from A to -A in VHDL. I have some questions about two's complement notation. For instance if I have A(8 bit) = 5, in binary 0000 0101. From online sources I realized that to translate it into two's complement negative form, I need to invert all bits adding 1 at the end:

0000 0101 --> 1111 1010 + 0000 0001 = 1111 1011 and this represents -A=-5;

My doubts now is about this final binary form that can represent -5 and 251, how can I recognize if it is -5 or 251?

By the way, this method is not so simply to be described in VHDL. Do you know if there is any simpler method?

7
  • Please use Google to find the difference between signed and unsigned arithmetic. Commented May 16, 2018 at 15:53
  • 1
    Easy. Use the numeric_std library which provides signed and unsigned types. Look at the type declaration for that signal; if it's signed(7 downto 0) then it's -5; if it's unsigned(7 downto 0) then it's 251. Commented May 16, 2018 at 16:30
  • @BrianDrummond @JHBonarius , for the exam my teacher said that we can only use bit statement, so I had the doubt! btw, how can I recognize it not in VHDL? if I have 1111 1011 how can I know if it rapresents -5 or 251? PS: is there a simply method to convert a positive binary number into his negative form? Commented May 16, 2018 at 16:36
  • 1
    numeric_bit provides signed/unsigned types too... PS ... yes. Commented May 16, 2018 at 16:38
  • 3
    Without an appropriate package, ie: numeric_std or numeric_std_bit, there is no way to designate the value as signed or unsigned - this is indeed the purpose of strong typing and the types signed and unsigned. The type is part of the documentation of the code. If your instructor insists on type bit - they are doing an academic exercise. OK for school. Not ok in the professional world. Commented May 16, 2018 at 17:52

1 Answer 1

1

Intuitively my reasoning would be: you are using two's complement, which is a signed number representation, therefore negative values exist. If negative values exist, you need a sign bit. That will leave 7-bits for the magnitude: hence you can only represent a value between -128 and 127. Value 251 is not within this range: It cannot be represented using 8-bits two's complement notation. Thus only -5 is valid.

The easiest way to realize two's complement sign inversion in VHDL is using the numeric_bit package.

library ieee;
entity bit_inv is
    generic(width : positive);
    port(
        A     : in  bit_vector(width-1 downto 0);
        A_inv : out bit_vector(width-1 downto 0));
end entity;

architecture rtl of bit_inv is
    use ieee.numeric_bit.all;
begin
    A_inv <= bit_vector(-signed(A));
end architecture;

entity bit_inv_tb is end entity;

library ieee;
architecture beh of bit_inv_tb is
    use ieee.numeric_bit.all;
    constant width : positive := 8;
    signal A, A_inv : bit_vector(width-1 downto 0);
begin
    DUT : entity work.bit_inv
        generic map(width => width)
        port map(A=>A, A_inv =>A_inv);

    test: process begin
        A <= bit_vector(to_signed(5,width));
        wait for 1 ns;
        assert to_integer(signed(A_inv)) = -5 report "A_inv is not equal to -5" severity failure;
        wait;
    end process;
end architecture;
Sign up to request clarification or add additional context in comments.

2 Comments

I can't use signed or unsigned and I wrote this code but I can't test it cause testbench goes in loop with every code I write: link (too long for the comment) My teacher made a similar code using std_logic statements but it seems strange cause I think we can associate "11111111" to a std_logic_vector signal; what do you think about?
@DarkPassenger: you were asking for a simpler method: here it is.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.