Verilog Code for Count Zeroes in Binary Vector

Count Zeroes in Binary Vector:

Suppose you have a binary number, how do you count the number of zeroes in it? 
There is one way to do it.
Let's take a 32 bit binary number. The output of our design will have a width of 5 bits, to include the maximum  value of output.
In this code we used a loop,which run 32 times and each time it will check.If zero will come then it will count 1
and if zero will come again then output will incremented by 1.
After 32th bit it will show the output in binary form.

Verilog Code for Count Zeroes in Binary Vector:

module leading_zeroes(count,a); 
input [31:0]a; 
output reg [4:0]count; 
integer i; 
always@(a) 
begin 
count=0; 
for(i=0;i<32;i=i+1) 
begin  
if(a[i]==0) 
begin   
count=count+1;   
end 
end 
$display("%d",count);   
end 
endmodule

OUTPUT: 


Post a Comment

0 Comments