explicit function to enumarate N bits words with M ones



Hi, I was wandering whenever it exists a known method to explicitely enumerate all the N bits words having 0 <= M <= N ones, i.e. something like

F(N, M, i)

where i < binomial(N, M) means the i-th word.
all the words does not have to be sorted, I just need to have them enumerated (e.g. F(N,M,i) does not have to be less than F(N,M,i+1)).

The problem arises because exhaustive (brute-force) search isn't feasible for large values of N (and not trivial values of M), and a recursive approach is rather slow as well.

My text books give me no insight on this... any ideas?

Thank you,
Carlo Alberto
.