我在geeksforgeeks网站上找到了一个函数,可以查找给定集合的所有子集。但是我不确定嵌套的for循环中的if语句是在检查什么。我知道它使用了按位与运算符,但我不明白它如何帮助确定哪些元素在任何迭代中包含或不包含。
import java.io.IOException;
public class Main
{
// Print all subsets of given set[]
static void printSubsets(char set[])
{
int n = set.length;
// Run a loop for printing all 2^n
// subsets one by obe
for (int i = 0; i < (1<<n); i++)
{
System.out.print("{ ");
// Print current subset
for (int j = 0; j < n; j++)
//???what is this checking?????
if ((i & (1 << j)) > 0)
System.out.print(set[j] + " ");
System.out.println("}");
}
}
// Driver code
public static void main(String[] args)
{
char set[] = {'a', 'b', 'c'};
printSubsets(set);
}
}