How to generate all $n$-bit numbers with certain bits fixed?
Practically speaking, here is one thing you could try:


Make a list $A[1],\ldots,A[m]$ of powers of two corresponding to free bits.
Generate a list of all XORs of elements from $A[i]$ (in the correct order), XORed to a mask consisting of the fixed bits.


If you don't care about the order, then an efficient way to generate the list is using a Gray code. Otherwise, you could use a recursive procedure.