I have t开发者_如何学运维ried writting the following function to generate all combinations of a string by translating the algorithm from my algorithm text. But it keeps print entire string in the output for all the combinations.
len = strlen(str);
for(i=0;i<pow(2,len);i++)
{
for(j=0;j<len;j++)
{
if(i && (0x1 << j))
{
cout<<str[j];
}
}
cout<<endl;
}
Thanks you all.
Since you want to check if the j
th bit is set in variable i
you need to use the bitwise &
operator and not the logical &&
:
if(i && (0x1 << j))
^^
精彩评论