First, find the definition of an abstract problem: you have a 8-bit long vector type, which is a combination of your 8 input signals. For each signal, you have a bitvector value, for example 10000000 (first signal) or 00100000 (third signal). These values โโare given. You want to generate the following values โโ(I forgot the trivial ones):
r[0] = 10100000 r[1] = 01010000 r[2] = 00101000 r[5] = 00010100 r[8] = 01011010 r[9] = 00111010 r[10] = 00011100 r[11] = 00001101 r[12] = 00100100 r[14] = 01111110 r[15] = 00111100
Now we want to find the minimum combinations ( XOR execution) to generate these values. This is an optimization problem. I will not do full proof for the least amount of XOR executions here, but this is what I get:
int i1 = s02 ^ s04; // 01010000 int i2 = s03 ^ s05; // 00101000 int i3 = s04 ^ s06; // 00010100 int i4 = s05 ^ s07; // 00001010 int i5 = s03 ^ s06; // 00100100 int i6 = i1 ^ i4; // 01011010 int i7 = i2 ^ i3; // 00111100 int i8 = s06 ^ s07; // 00000110 r[0] = s01 ^ s03; r[1] = i1; r[2] = i2; r[5] = i3; r[8] = i6; r[9] = i7 ^ i8; r[10] = i3 ^ s05; r[11] = i4 ^ i8 ^ s08; r[12] = i5; r[14] = i6 ^ i5; r[15] = i7;
14 XOR s.
To formulate a general algorithm: you start with Set S={10000000, 01000000, ... , 00000001} . You need a weighing function that tells you the value of your set. Define this as: The XOR number needed to calculate all the target values โโfrom the values โโin S without saving any additional time values plus the number of values โโin S minus 8 (initial values). The first part of the weighing function can be implemented using brute force (find all possible combinations for the target value that use each value in S no more than once, select the one that runs with the smallest XOR ).
To optimize the value of your weight function, you combine the two values โโfrom S with XOR and add them to S , providing S1 . Choose those two values โโthat give the lowest new value of the weight function (again, this can be determined by brute force). S1 now has another value (which will be a temporary value, such as the i values โโin my solution). To create this value, one XOR is required (therefore, the weight function calculates the number of values โโin S ).
Continue this step until you find a new value to add to S , which reduces the value of the weighing function. The resulting set contains the initial values โโplus all the temporary values โโthat you must calculate. The steps you take will tell you how to calculate the closest values.
This is a greedy algorithm. It will not necessarily find the minimum amount of XOR s, but it will show you a simple way to at least get a good solution. Maybe the algorithm actually always finds the best solution, but this should be proved. If you want to be absolutely sure, you can make a complete detour of all possible steps that reduce the value of the weight function, starting with the initial values โโof S This will be a tree traversal, and the tree will be finite - since the value cannot go below 0 - so it will definitely resolve.