A bitstring is defined by a sequence of ones and zeros, e.g. "0101110111". Equivalently, it is defined by an integer as its binary representation.
I want to calculate a certain function of a bitstring for all bitstrings of a certain length $l$. This is equivalent to calculating that function for all integers from 0 up to $2^l-1$
I want to optimize my code by making less computation. I have noticed that if the difference between previous and next bitstring is only in 1 arbitrary bit (e.g. for "110010" and "111010" only the 3rd bit differs), the result of the function for the previous bitstring can be reused to significantly decrease the computation cost of the function for the next bitstring.
Question: Is there an easy algorithm to loop over all bitstrings of length $l$ in such a way, that the difference between any two consecutive bitstrings is only in 1 bit.
Bad example of length 2: 00 -> 01 -> 10 -> 11: the second step has difference of 2 bits
Good example of length 2: 00 -> 01 -> 11 -> 10: all steps have difference of only 1 bit