1

I have a 4 bits std_logic_vector whose values are represented in 2's complement. And I want to extract it's unsigned value

signal FOURbits_2scomplement : std_logic_vector(3 downto 0);

signal THREEbits_number : unsigned(2 downto 0);

THREEbits_number <= what_to_do(FOURbits_2scomplement); -- here !

How can we convert the given 4 bits that we encoded/represent in 2's complement to a 3-bit binary encoded binary value?

I am stuck because of the width...

3
  • 1
    So if the value for instance is 1011 in 2's complement, which is -5, you want your unsigned number to be 5 or 101 respectively? Am I right there, or do you want to achieve something different? Commented Jun 4, 2021 at 10:22
  • In fact I would like to test if the value is smaller than 2 in which case I'll output a 000 for all number smaller thant 2, but else if the value is positive for example le'ts say +4 then output 100 Commented Jun 4, 2021 at 10:43
  • So, yeah, I want simply convert a positive 2's complement number of 4 bits into it's normal/unsigned binary number of 3 bits Commented Jun 4, 2021 at 11:00

1 Answer 1

0

I would recommend you to use the numeric_std-package. It comes with types signed and unsigned. You can now just cast the std_logic_vector to one of these types (I still don't know exactly what you are going to do, but this explaination should be sufficient for you) and then compare if the value is greater or less than 2.

To understand what I just wrote, have a look at the table at this site.

If I got you wrong and you want to convert the number from 2's complement to binary, refer to the accepted answer of this question. In short, it is just inverting, adding 1 and cut the sign bit.

Sign up to request clarification or add additional context in comments.

Comments

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.