Given a list of input binary numbers in A, and a list of output binary numbers in B, seek one value for all X that satisfies the bitwise AND. i.e. A and X = B where A has 6 set bits, B has zero to 6 set bits, and X has 12 set bits. All numbers in A, B and X are 128 bits long.
Similar to this question: Most efficient method of generating a random number with a fixed number of bits set but I also need that random number to produce known binary results when bitwise-and-ed with a known set of binary numbers.
A naive idea: Generate a random 128 bit number X with 12 bits set, then test it against all the numbers in A to see if it generates B. If it doesn't, shuffle the bits in X and try again.
I know there must be a better algorithm.
Clarification: B is the same as A, except that some or all of the bits set (1s) in A have now been randomly set to 0 in B.
Update on my naive idea:
I just figured out that whenever a bit in A is 1, corresponding bit in X will be equal to corresponding bit in B since 1 AND xBit = bBit. When the bit in A is 0, then the xBit is unknown (0 or 1). So for each number in A, I can obtain string X made up of 1s, zeros and unknown y. e.g. yy00011y10010y10... then I could compare all these strings X to one another and find a "fit" for a unique X by replacing the y with 1s and 0s. I'm not sure if this is the best way, but it is probably better than guessing and checking. Any thoughts on this, or how to find such a fit? Thanks