Search for X-flipped strings
Consider, for example, the case when N = 10, X = 4, and the initial row:
initial: 0011010111
then this will be an example of an inverted X line:
flipped: 0000111111
because 4 bits are different. If you XOR two lines, you will get:
initial: 0011010111 flipped: 0000111111 XOR-ed: 0011101000
where four bits (units) in the XOR-ed line indicate the location of the 4 bits that have been flipped.
Now think about it back. If you have an initial string and a string with four bits set, you can generate an X-flipped string using XOR-ing:
initial: 0011010111 4 bits : 0011101000 XOR-ed: 0000111111
So, if you generate every binary string of length N with X set bits, and you are XOR with an inline string, you get all the lines with X-flipped upside down.
initial 4 bits XOR-ed 0011010111 0000001111 0011011000 0000010111 0011000000 0000100111 0011110000 ... 1110010000 1101000111 1110100000 1101110111 1111000000 1100010111
Generation of all N-length strings using X bit-bits can be performed, for example. with gosper hack . In the code example below, I am using the reverse lexicographic order function that I originally wrote for this answer .
Double click
If the bits can be flipped twice, it is possible that the X-flipped string does not have X bits other than the original string, but only X-2, because one bit was flipped and then returned to its original state, Or X-4 if a bit was flipped 4 times or two different bits were flipped twice. In fact, the number of different bits can be X, X-2, X-4, X-6 ... up to 1 or 0 (depending on whether X is odd or even).
So, to generate all lines with X-flipped, you generate all lines with inverted bits of X, then all lines with inverted bits of X-2, then X-4, X-6 ... to 1 or 0.
If X> N
If X is greater than N, then obviously some bits will be flipped more than once. The method for generating them is the same: you start with X, count to X-2, X-4, X-6 ... but only generate lines for & le; N. In practice, you start with N or N-1, depending on whether XN is even or odd.
Total row count
The number of N-length strings with inverted bits of X is equal to the number of N-length strings with bits of the X set, which is the Binomial coefficient N choose X Of course, you have to consider lines with X-2, X-4, X-6 ... inverted bits too, so the total:
(N choose X) + (N choose X-2) + (N choose X-4) + (N choose X-6) + ... + (N choose (1 or 0))
In the case where X is greater than N, you start with N choose N or N choose N-1 , depending on whether XN is even or odd.
For your example with N = 3 and X = 2, the total is:
(3 choose 2) + (3 choose 0) = 3 + 1 = 4
In the above example with N = 10 and X = 4, the total number:
(10 choose 4) + (10 choose 2) + (10 choose 0) = 210 + 45 + 1 = 256
For example, in another answer with N = 6 and X = 4, the correct number is:
(6 choose 4) + (6 choose 2) + (6 choose 0) = 15 + 15 + 1 = 31
Code example
This piece of JavaScript code generates binary string sequences in reverse lexicographic order (so that the given bits move from left to right), and then prints the received expanded lines and the total for the examples described above:
function flipBits(init, x) { var n = init.length, bits = [], count = 0; if (x > n) x = n - (x - n) % 2;