Inside a clocked process I count the set bits of an 8-bit input with a for loop: ones <= ones + 1; whenever data(i) = '1', where ones is a signal that I clear at the top of the process. For the input "11110000" the result is wrong: it increases by one on every clock edge instead of settling at 4. A version using a variable and := gives 4 as expected.
What is the real difference between a signal and a variable in a process, how does each map to hardware, and when should I prefer one over the other?




