I do not understand how the following operations 2s – 1 and 1 - 2s are performed in the following expressions:
R <= (s & '0') - 1;
L <= 1-(s & '0');
Considering the fact that R and L are of type signed(1 downto 0) and s of type std_logic. I have extracted them from a vhdl code snippet in my professor's notes.
What I understand (or at least consider to understand – premises of my reasoning)
- The concatenation with the
0literal achieves the product by2(That is what shifting to the left does). - The concatenation also achieves a
std_logic_vectorof 2 bits (not so sure about that, I inferred this from a comment in the following StackOverflow question - "
std_logic_vectoris great for implementing data buses, it’s useless for performing arithmetic operations" - source: vhdlwhiz.
What baffles me:
- What type does the compiler interpret the
1literal is?- An integer? If so, can an
integerbe used without casting in an arithmetic expression with astd_logic_vector? This option doesn't seem very plausible to me... - Assuming the fact that the
(s & '0')is indeed interpreted as astd_logic_vector(second premise) it also comes to my mind the possibility that the compiler, based on the type of the other operand in the expression (i.e.,(s & '0')), inferred1to be of typestd_logic_vectoras well. However, even though both(s & '0')and1were interpreted asstd_logic_vectorthey should not be behaving correctly according to my third premise.A thought that comes to my mind in order tu justify the possibility of both operands being of type
std_logic_vectoris that both(s & '0')and1are implicitly casted tosignedby the compiler because it acknowledges the fact the signal in which the result is stored is of typesigned. This, doesn't seem to make sense to me (supposesis equal to1):R <= (s & '0') - 1;Both operands are converted to
std_logic_vectors(1 downto 0)R <= "10" - "01"Now, if the contents of the std_logic_vectors were interpreted as
signedthe result of the subtraction would beR <= (-2) - (1) = -3
- An integer? If so, can an
As you can tell I am really confused. I believe we've only scratched the surface when it comes to discussing data types in class and I am encountering a lot of problems when solving problems because choosing the wrong data types.
I apologize for any lack of clarity in my questions; they reflect my current understanding of the subject. Thank you for your patience.