Consider the following VHDL process using numeric_std to add two unsigned vectors:
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity add_unsigned is
port(
a, b : in unsigned(3 downto 0);
sum : out unsigned(4 downto 0)
);
end add_unsigned;
architecture behavior of add_unsigned is
begin
process(a, b) is
begin
sum <= ('0' & a) + ('0' & b);
end process;
end behavior;If a = "1010" and b = "0111", what is the value of sum after the process runs?
library ieee; use ieee.std_logic_1164.all; use ieee.numeric_std.all; entity add_unsigned is port( a, b : in unsigned(3 downto 0); sum : out unsigned(4 downto 0) ); end add_unsigned; architecture behavior of add_unsigned is begin process(a, b) is begin sum <= ('0' & a) + ('0' & b); end process; end behavior;
Remember to extend both operands to 5 bits before adding.
Concatenating '0' to 4-bit vectors makes them 5 bits wide. Adding 01010 (a) and 00111 (b) as 5-bit unsigned numbers results in 10001 (decimal 17).
Given the following VHDL snippet:
library ieee; use ieee.std_logic_1164.all; use ieee.numeric_std.all; signal a : signed(3 downto 0) := "0101"; signal b : unsigned(3 downto 0) := "0011"; signal c : signed(4 downto 0); begin c <= a + b;
What happens when you try to compile this code?
Check if numeric_std allows adding signed and unsigned directly.
In numeric_std, '+' operator is overloaded for operands of the same type only. Adding signed and unsigned directly causes a compilation error.
Analyze this VHDL code snippet:
signal a : std_logic_vector(3 downto 0) := "1101"; signal b : signed(3 downto 0); signal c : integer; begin b <= signed(a); c <= to_integer(b);
What is the value of c after execution, and why might it be unexpected?
Remember how signed interprets the most significant bit as sign.
The vector "1101" as signed is -3 in two's complement (because MSB=1 means negative). So to_integer returns -3.
Which of the following VHDL statements correctly converts an integer i to an unsigned vector u of length 8 using numeric_std?
Check the required arguments for to_unsigned.
to_unsigned requires two arguments: the integer and the vector length. Option B is correct.
You have two unsigned vectors a and b each 7 bits wide. You want to store their sum in an unsigned vector sum. How many bits should sum have to always hold the result without overflow?
Think about the maximum value of two 7-bit unsigned numbers added together.
Two 7-bit unsigned numbers max at 127 each. Their sum max is 254, which fits in 8 bits (max 255). So 8 bits are needed.
