Challenge - 5 Problems
Signed and Unsigned Arithmetic Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of signed addition in VHDL
What is the output of this VHDL process when adding two signed vectors?
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity test is
end entity;
architecture behavior of test is
signal a, b, c : signed(3 downto 0);
begin
process
begin
a <= to_signed(3, 4);
b <= to_signed(-2, 4);
wait for 10 ns;
c <= a + b;
wait;
end process;
end architecture;VHDL
library ieee; use ieee.std_logic_1164.all; use ieee.numeric_std.all; entity test is end entity; architecture behavior of test is signal a, b, c : signed(3 downto 0); begin process begin a <= to_signed(3, 4); b <= to_signed(-2, 4); wait for 10 ns; c <= a + b; wait; end process; end architecture;
Attempts:
2 left
💡 Hint
Remember that signed vectors use two's complement representation.
✗ Incorrect
Adding 3 and -2 results in 1. The 4-bit signed representation of 1 is "0001".
❓ Predict Output
intermediateUnsigned subtraction result in VHDL
What is the value of signal c after this VHDL process runs?
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity test is
end entity;
architecture behavior of test is
signal a, b, c : unsigned(3 downto 0);
begin
process
begin
a <= to_unsigned(5, 4);
b <= to_unsigned(3, 4);
wait for 10 ns;
c <= a - b;
wait;
end process;
end architecture;VHDL
library ieee; use ieee.std_logic_1164.all; use ieee.numeric_std.all; entity test is end entity; architecture behavior of test is signal a, b, c : unsigned(3 downto 0); begin process begin a <= to_unsigned(5, 4); b <= to_unsigned(3, 4); wait for 10 ns; c <= a - b; wait; end process; end architecture;
Attempts:
2 left
💡 Hint
Unsigned subtraction cannot represent negative numbers.
✗ Incorrect
5 - 3 = 2, which is "0010" in 4-bit unsigned binary.
❓ Predict Output
advancedResult of mixed signed and unsigned addition
What is the output of signal c after this VHDL process?
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity test is
end entity;
architecture behavior of test is
signal a : signed(3 downto 0);
signal b : unsigned(3 downto 0);
signal c : signed(4 downto 0);
begin
process
begin
a <= to_signed(-3, 4);
b <= to_unsigned(5, 4);
wait for 10 ns;
c <= resize(a, 5) + signed(resize(b, 5));
wait;
end process;
end architecture;VHDL
library ieee; use ieee.std_logic_1164.all; use ieee.numeric_std.all; entity test is end entity; architecture behavior of test is signal a : signed(3 downto 0); signal b : unsigned(3 downto 0); signal c : signed(4 downto 0); begin process begin a <= to_signed(-3, 4); b <= to_unsigned(5, 4); wait for 10 ns; c <= resize(a, 5) + signed(resize(b, 5)); wait; end process; end architecture;
Attempts:
2 left
💡 Hint
Convert unsigned to signed before adding, and consider bit width.
✗ Incorrect
Resizing and converting unsigned 5 to signed 5-bit gives "00101" (5). Adding -3 ("11101" in 5 bits) plus 5 equals 2 ("00010").
❓ Predict Output
advancedOverflow behavior in signed addition
What is the value of signal c after this VHDL process?
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity test is
end entity;
architecture behavior of test is
signal a, b, c : signed(3 downto 0);
begin
process
begin
a <= to_signed(7, 4); -- max positive for 4-bit signed
b <= to_signed(1, 4);
wait for 10 ns;
c <= a + b;
wait;
end process;
end architecture;VHDL
library ieee; use ieee.std_logic_1164.all; use ieee.numeric_std.all; entity test is end entity; architecture behavior of test is signal a, b, c : signed(3 downto 0); begin process begin a <= to_signed(7, 4); -- max positive for 4-bit signed b <= to_signed(1, 4); wait for 10 ns; c <= a + b; wait; end process; end architecture;
Attempts:
2 left
💡 Hint
4-bit signed addition can overflow and wrap around.
✗ Incorrect
Adding 7 + 1 in 4-bit signed causes overflow, resulting in -8 ("1000").
🧠 Conceptual
expertError type from mixing signed and unsigned without conversion
What error will this VHDL code produce?
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity test is
end entity;
architecture behavior of test is
signal a : signed(3 downto 0);
signal b : unsigned(3 downto 0);
signal c : signed(4 downto 0);
begin
process
begin
a <= to_signed(2, 4);
b <= to_unsigned(3, 4);
wait for 10 ns;
c <= resize(a, 5) + b; -- no conversion of b to signed
wait;
end process;
end architecture;VHDL
library ieee; use ieee.std_logic_1164.all; use ieee.numeric_std.all; entity test is end entity; architecture behavior of test is signal a : signed(3 downto 0); signal b : unsigned(3 downto 0); signal c : signed(4 downto 0); begin process begin a <= to_signed(2, 4); b <= to_unsigned(3, 4); wait for 10 ns; c <= resize(a, 5) + b; -- no conversion of b to signed wait; end process; end architecture;
Attempts:
2 left
💡 Hint
Check the types of operands in the addition expression.
✗ Incorrect
VHDL does not allow adding signed and unsigned types without explicit conversion, causing a type mismatch error.
