number of ones in array

前端 未结 2 432
悲&欢浪女
悲&欢浪女 2021-01-20 05:23

I am trying to count the number of ones in a 4-bit binary number in Verilog, but my output is unexpected. I\'ve tried several approaches; this is the one I think should work

2条回答
  •  说谎
    说谎 (楼主)
    2021-01-20 06:15

    First, you can't assign the variable twice.

    Second, your range is off, 2 bits can only go from 0 to 3. You need a 3 bit output to count up to 4.

    This is more like what you need:

    module ones(
      output wire [2:0] one,
      input wire [3:0] in
    );
    
    assign one = in[3]+in[2]+in[1]+in[0] ;
    
    endmodule
    

提交回复
热议问题