signed and unsigned arithmetic in VHDL - Time & Space Complexity
We want to understand how the time it takes to do signed and unsigned arithmetic changes as the size of the numbers grows.
How does the number of bits affect the work needed for addition or subtraction?
Analyze the time complexity of the following code snippet.
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity ArithmeticExample is
port(
a, b : in unsigned(7 downto 0);
sum : out unsigned(7 downto 0)
);
end ArithmeticExample;
architecture Behavioral of ArithmeticExample is
begin
sum <= a + b;
end Behavioral;
This code adds two 8-bit unsigned numbers and outputs the result.
In hardware, addition works bit by bit, starting from the least significant bit.
- Primary operation: Adding each bit with carry.
- How many times: Once per bit, so 8 times for 8 bits.
As the number of bits increases, the number of bit additions grows linearly.
| Input Size (bits) | Approx. Operations |
|---|---|
| 8 | 8 bit additions |
| 16 | 16 bit additions |
| 32 | 32 bit additions |
Pattern observation: Doubling the bits doubles the number of bit operations.
Time Complexity: O(n)
This means the time to add numbers grows in a straight line with the number of bits.
[X] Wrong: "Adding signed numbers takes twice as long as unsigned numbers."
[OK] Correct: Both signed and unsigned addition use the same bit-by-bit process, so their time grows the same way with bit size.
Knowing how arithmetic operations scale helps you understand hardware design and performance, a useful skill in many tech roles.
"What if we changed addition to multiplication? How would the time complexity change?"
Practice
Solution
Step 1: Understand number representation types
Signed types allow representation of both positive and negative numbers, unlike unsigned which only represent zero and positive numbers.Step 2: Identify correct VHDL type for negatives
In VHDL, thesignedtype fromnumeric_stdis used for signed arithmetic, supporting negative values.Final Answer:
signed -> Option BQuick Check:
Negative numbers need signed type [OK]
- Using unsigned for negative numbers
- Using std_logic_vector without conversion
- Confusing bit_vector with numeric types
Solution
Step 1: Identify the correct type for unsigned 4-bit signal
Theunsignedtype fromnumeric_stdis used for unsigned arithmetic and must have 4 bits indexed from 3 downto 0.Step 2: Check each option
signal data : signed(3 downto 0); uses signed type, which is for signed numbers. signal data : std_logic_vector(4 downto 0); has 5 bits (4 downto 0). signal data : bit_vector(3 downto 0); uses bit_vector, which is not numeric.Final Answer:
signal data : unsigned(3 downto 0); -> Option DQuick Check:
Unsigned 4-bit signal uses unsigned(3 downto 0) [OK]
- Using signed instead of unsigned
- Incorrect bit range (off by one)
- Using std_logic_vector without conversion
result after execution?signal a : signed(3 downto 0) := "1101"; -- -3 in decimal signal b : unsigned(3 downto 0) := "0011"; -- 3 in decimal signal result : signed(4 downto 0); result <= a + signed(resize(b, 5));
Solution
Step 1: Convert signals to decimal
ais signed "1101" which is -3 in decimal.bis unsigned "0011" which is 3 in decimal.Step 2: Resize and add values
bis resized to 5 bits and converted to signed, so 3 remains 3. Adding -3 + 3 = 0.Final Answer:
0 -> Option AQuick Check:
-3 + 3 = 0 [OK]
- Adding signed and unsigned directly without conversion
- Misinterpreting binary signed values
- Ignoring resize causing wrong bit width
signal x : signed(3 downto 0) := "0110"; signal y : unsigned(3 downto 0) := "0011"; signal z : signed(3 downto 0); z <= x + y;
Solution
Step 1: Check types in addition
In VHDL, you cannot add a signed and an unsigned type directly without converting one to the other's type.Step 2: Identify missing conversion
The code addssignedxandunsignedydirectly, which causes a type mismatch error.Final Answer:
Cannot add signed and unsigned directly without conversion -> Option AQuick Check:
Signed + unsigned needs conversion [OK]
- Adding signed and unsigned without conversion
- Assuming std_logic_vector works for arithmetic
- Ignoring type mismatch errors
b from a signed 5-bit number a in VHDL. Which is the correct way to perform this operation and store the result in a signed 6-bit signal result?Solution
Step 1: Understand type and size requirements
To subtract unsignedbfrom signeda, both must be the same type and size. The result signal is 6 bits signed.Step 2: Convert and resize operands properly
Resizeato 6 bits signed, convert and resizebto signed 6 bits, then subtract.Final Answer:
result <= resize(a, 6) - signed(resize(b, 6)); -> Option CQuick Check:
Resize and convert unsigned to signed before subtracting [OK]
- Subtracting without resizing operands
- Not converting unsigned to signed before subtraction
- Assigning result to smaller bit width
Ready to prove it?
Pass the test and this pattern moves unknown
