I’ve been given a tip the following code will work, and it does, however I am using it in a little research project and need to thoroughly understand what exactly is the principle behind it.
for i:=0 to (PNum-1) do begin
for j:=0 to (SMax-1) do begin
write(f, ((i shr j) and 1));
end;
writeln(f);
end;
Basically, it generates all PNum variations of SMax-symbol long string containing 0 and 1. My question is, what does ((i shr j) and 1) (shr being right bit shift in Pascal) do?
Thank you in advance.
The expression
extracts the
j-th bit from the binary representation of `i´ (bits counting from zero from the right).Example:
Note: the algorithm builds all combinations of
[0,1]ifPNum = 2^SMaxand not all permutations.