mercredi 15 juin 2016

Efficiently generating a random binary number with fixed set bits and known bitwise AND results

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.




Aucun commentaire:

Enregistrer un commentaire