Altera_Forum
Honored Contributor
15 years agoto find out the number of "1"
hi,everyone
i need to find out the number of "1" in a input (a), now ,i thik i could add every bit of input (a) to get the resut, just like : assign b=a[0]+a[0]+a[1]+a[2]+a[3]+a[4]; -----code(1) so , if a==5'd10==5'b01010 then b=2 , it's right but ,now, the width of a is 2023 ,it is too large. so , for me ,it is a big trouble for me to write like code(1) ,it will cost too much time. so i wright like this: module jj(data_in,weight_out); parameter IN_WIDTH=10; input [IN_WIDTH-1:0 ] data_in; output [11:0] weight_out; reg [11:0] a; always @ (*) begin integer i; for(i=0;i<IN_WIDTH;i=i+1) a<=a+data_in[i]; end assign weight_out=a; endmodule but , this logic is not right.and a combinational loop occurs. any convenient and right method? thank you!