TransWikia.com

XOR combination between the bits of a string

Mathematica Asked on March 2, 2021

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.

One Answer

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

Correct answer by kglr on March 2, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP