Monday, January 21, 2013

Printing all possible subsets using BitMask

#include<stdio.h>

void PrintAllSubset(int n)
{
int mask,pos,i,j,noOfSubSet = 1<<n; // subset size 2^n subsets
for(i=1;i<noOfSubSet;i++)
 {
for(j=0;j<n;j++)
if((i&(1<<j)) > 0)
printf("%d ",j+1);
printf("\n");
}
}
int main()
{
int n;
while(scanf("%d",&n) == 1)
{
PrintAllSubset(n);
}
return 0;
}  

No comments:

Post a Comment