VHDL interview question - detecting if a number can be divided by 5 without remainder
Doing a remainder operation in serial fashion is actually quite easy. The key assumption is that the data comes in MSB-first if it's serial. You only need N states to compute a remainder modulo N. Start in the "0" state and if you end up in the "0" state after the last bit (it doesn't matter how many bits there are), the remainder is zero.







simulate this circuit &ndash; Schematic created using CircuitLab

Think about how you'd do long division if the only thing you needed to keep track of was the remainder:

process (clk)
begin
  if rising_edge(clk) then
    if reset = 1 then
      state &lt;= 0;
    else
      if (state &amp; din) &gt;= N then
        state &lt;= (state &amp; din) - N;
      else
        state &lt;= state &amp; din;
      end if;
    end if;
  end if;
end process;