1
$\begingroup$

Given an integer n, we can construct $2^n$ strings of length n. We can take the first element for each of these strings and create a list. In total 'n' such lists are possible. But now I need to create lists that are the XOR combinations of the n lists created. If n is 3 and abc is the string then I need to calculate a$\oplus$b, a$\oplus$c, c$\oplus$b, a$\oplus$b$\oplus$c. How do I do this for a general n?

Here's the code so far...

n = 4; s = Tuples[{0, 1}, n]; a = Table[s[[i, j]], {j, 1, n}, {i, 1, 2^n}]

a gives the first four indices of all the strings. Now I need to calculate all the XOR combinations possible between these four lists.

$\endgroup$
3
  • 1
    $\begingroup$ does Mod[Plus@##, 2] & @@@ Subsets[a, {2, n}] give what you need? $\endgroup$ – kglr Dec 8 '20 at 7:26
  • 1
    $\begingroup$ or (the same thing) Mod[Plus@##, 2] & @@@ Subsets[Transpose[s], {2, n}]? $\endgroup$ – kglr Dec 8 '20 at 7:27
  • 1
    $\begingroup$ or more concisely, Mod[Plus @@@ Subsets[Transpose[s], {2, n}], 2] $\endgroup$ – kglr Dec 8 '20 at 7:47
2
$\begingroup$

Maybe

xOrCombinations[n_] := BitXor @@@ Subsets[Transpose @ Tuples[{0, 1}, n], {2, n}]

xOrCombinations[4] // MatrixForm // TeXForm

$$\left( \begin{array}{cccccccccccccccc} 0 & 0 & 0 & 0 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 0 & 0 & 0 & 0 \\ 0 & 0 & 1 & 1 & 0 & 0 & 1 & 1 & 1 & 1 & 0 & 0 & 1 & 1 & 0 & 0 \\ 0 & 1 & 0 & 1 & 0 & 1 & 0 & 1 & 1 & 0 & 1 & 0 & 1 & 0 & 1 & 0 \\ 0 & 0 & 1 & 1 & 1 & 1 & 0 & 0 & 0 & 0 & 1 & 1 & 1 & 1 & 0 & 0 \\ 0 & 1 & 0 & 1 & 1 & 0 & 1 & 0 & 0 & 1 & 0 & 1 & 1 & 0 & 1 & 0 \\ 0 & 1 & 1 & 0 & 0 & 1 & 1 & 0 & 0 & 1 & 1 & 0 & 0 & 1 & 1 & 0 \\ 0 & 0 & 1 & 1 & 1 & 1 & 0 & 0 & 1 & 1 & 0 & 0 & 0 & 0 & 1 & 1 \\ 0 & 1 & 0 & 1 & 1 & 0 & 1 & 0 & 1 & 0 & 1 & 0 & 0 & 1 & 0 & 1 \\ 0 & 1 & 1 & 0 & 0 & 1 & 1 & 0 & 1 & 0 & 0 & 1 & 1 & 0 & 0 & 1 \\ 0 & 1 & 1 & 0 & 1 & 0 & 0 & 1 & 0 & 1 & 1 & 0 & 1 & 0 & 0 & 1 \\ 0 & 1 & 1 & 0 & 1 & 0 & 0 & 1 & 1 & 0 & 0 & 1 & 0 & 1 & 1 & 0 \\ \end{array} \right)$$

Also

xOrCombinations2[n_] := Mod[Plus @@@ Subsets[Transpose@Tuples[{0, 1}, n], {2, n}], 2]

xOrCombinations2[4] == xOrCombinations[4]
 True
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.