What I want to do is as follows:
Input: n, for example n = 3
Output: {000, 001, 010, 011, 100, 101, 110, 111}, generate all the subsets and I don't care the order of the subsets
I have implemented a algorithm:
for (long i = 0, max = 1 << n; i < max; i++) {
for (int j = 0; j < n; j++) {
// check if the j bit is set to 1
int isSet = (int) i & 1 << j;
if (isSet > 0) {
// if yes, set the corresponding jth bit of x as 1.
}
}
// add x to results;
}
I know Gray Code can do something like this. But I am wondering which one is the most efficient way to generate subsets?