I have been stuck in this problem for a long time.
I need to find a general code to collect all binary numbers which are at most n digits (<2^(n-1)
) with the kth digit equal to 0. For example, I need to find all binary numbers in the form of x0xx (4 digits with the second one equal to 0), then I need to write three For
loops and get the answer: {0000,0001,0010,0011,1000,1001,1010,1011}
. However, if I change the value of n or k, I need to modify my code, so now I am trying to find a general code for any n and k.
My idea is
ithzero = Compile[{{k, _Integer},{n,_Integer}}, nul = {};Do[nul = Append[nul, BaseForm[i, 2]*Boole[IntegerDigits[i, 2, n][[k]] == 0]], {i, 0, 2^n - 1}]];
However, I am not satisfied with this code, because it gives many 0s due to Boole
. By the way, I still don't have any idea about how to verify if x is in ithzero[n,k]. I thought 5\[Element]ithzero[5,2]
is a logic expression, but it isn't.
I am very grateful to any help!
ToString /@ Row /@ Select[Tuples[{0, 1}, n /. n -> 4], (#[[k /. k -> 2]] == 0) &]
$\endgroup$