Forum Discussion
Altera_Forum
Honored Contributor
14 years agoDetermining the index of the first 1 bit in a vector
Let's say I have a 64b wire, and I want to efficiently determine the index of the first 1 bit. For example: wire [63:0] sum; wire [5:0] index; assign sum = 64'b0000000111010110....1010...
Altera_Forum
Honored Contributor
14 years agoYou can use a function, like the one below and hope that Quartus synthesizes it efficiently.
It will produce a rather long combinational path, but I don't think that can be avoided. function [5:0] getIndex(input [63:0] data); integer i; for(i = 63; i >= 0; i = i - 1) begin if (data[63-i] == 1'b1) getIndex = i; end endfunction assign index = getIndex(sum);