Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
14 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

    no bugs, no hassle of spaghetti

    additionally you can add clock or change indexing

    
    library ieee;
    use ieee.std_logic_1164.all;
    use ieee.numeric_std.all;
    entity test is
    port(
    din : in  std_logic_vector(0 to 15);
    dout: out std_logic_vector(0 to 63)
    );
    end entity;
    architecture a of test is
    begin
    process(din)
    begin
    for i in 0 to 15 loop
        if din(i) = '0' then
              dout(i*4 to i*4+3) <= "0000";
         else
              dout(i*4 to i*4+3) <= std_logic_vector(to_unsigned(i,4));
         end if;
    end loop;
    end process;
    end a;
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    --- Quote Start ---

    no bugs, no hassle of spaghetti

    --- Quote End ---

    This solution is of course consuming much less logic resources, but not corresponding to the original specification - whatever may be it's purpose.