开发者

function gives wrong results

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 jth bit is set in variable i you need to use the bitwise & operator and not the logical &&:

if(i && (0x1 << j))
     ^^
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜