Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
15 years ago

Counting the position of ones in a binary vector

I want to design a combinatorial circuit for an fpga which will count the position of ones in a 16 bit vector.

eg1 if vector is 16'b0000_0000_0011_0101 then output should be

64'h0000_0000_0000_5420

eg2 if vector is 16'b0000_1010_0000_1010 then output should be

64'h0000_0000_0000_b931.

Please help.

Thanks in advance....

22 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    only as guide, not tested

    
    process(data_in)
    begin
    for i in 0 to 15 loop
        if data_in(i) = '0' then
              data_out(i*4:i*4+3) <= "0000";
         else
              data_out(i*4:i*4+3) <= std_logic_vector(to_unsigned(i,4));
         end if;
    end loop;
    end process;
    
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    This will not be straightforward to realise in a combinatorial circuit because what you describe is a rather sequential process: traverse the input vector from right to left, if you encounter a bit set to '1' add its position index into the next available output slot.

    This doesn't mean it can't be done combinatorially, but the result will be huge as it has to cater for all combinations (all 65356 of them ...).