Decoder and encoder design in VHDL - Time & Space Complexity
When designing decoders and encoders in VHDL, it's important to know how the time to process inputs grows as the number of inputs increases.
We want to understand how the operations inside these designs scale with input size.
Analyze the time complexity of the following decoder code snippet.
library ieee;
use ieee.std_logic_1164.all;
entity decoder_2to4 is
port(input : in std_logic_vector(1 downto 0);
output : out std_logic_vector(3 downto 0));
end decoder_2to4;
architecture Behavioral of decoder_2to4 is
begin
process(input)
begin
output <= (others => '0');
case input is
when "00" => output(0) <= '1';
when "01" => output(1) <= '1';
when "10" => output(2) <= '1';
when "11" => output(3) <= '1';
when others => output <= (others => '0');
end case;
end process;
end Behavioral;
This code converts a 2-bit input into a 4-bit output with only one bit set, acting as a decoder.
In this decoder design:
- Primary operation: The
casestatement checks the input value once per input change. - How many times: It runs once for each input change, no loops or recursion inside.
The dominant operation is the single selection among possible input cases.
As the input size grows (more bits), the number of output lines grows exponentially.
| Input Size (n bits) | Approx. Operations |
|---|---|
| 2 | 4 checks |
| 3 | 8 checks |
| 4 | 16 checks |
Pattern observation: The number of output lines doubles with each added input bit, so the checks grow exponentially.
Time Complexity: O(2^n)
This means the time to select the output grows exponentially as the number of input bits increases.
[X] Wrong: "The decoder checks only one output line regardless of input size."
[OK] Correct: Actually, the decoder must consider all possible input combinations, which double with each input bit, so the checks grow exponentially.
Understanding how decoder and encoder designs scale helps you explain hardware logic efficiency clearly and confidently in interviews.
"What if we replaced the case statement with a loop that checks each output line one by one? How would the time complexity change?"