How to find positions of set bits in a vector?

Here is a generic function that returns the positions of set bits in a vector and the total number of set bits. I found it useful, and you may too.

Vtool_Tips_Tricks_How_to_find_positions_of_set_bits_in_a_vector

For example, you can use this to represent which processing cores are available (1 = available, 0 = busy). The function identifies the indexes of ones,helping determine which cores can be assigned to new tasks.

Another example is dynamic routing in NoC. A bit vector can represent the availability of communication paths,and the function can identify the active bits in order to determine which paths to use.

For example, if data = 32’b1110111, the function outputs a queue with the positions: 0,1,2,4,5,6.

Now that you have indexes of set bits, you can do something like this:

Vtool_Tips_Tricks_How_to_find_positions_of_set_bits_in_a_vector_