我需要创建一个s由 1 和 0 组成的字符串。该字符串是根据函数返回的值创建的isSet。它总是返回1or 0。我不能简单地isSet放入一个if语句来添加1或添加0到字符串,因为程序不会编译。我是新手。我想知道是否有一种简单的方法来循环我的isSet函数,同时将它的返回值分配给我的字符串s,而它的内部bitsetfn。任何帮助表示赞赏。在 C++ 中
int isSet(unsigned int x, int i)
{
return ((x >> i) & 1);
}
string bitsetfn(int nbits, unsigned int x)
{
string s;
for (int i = 0; i < 16; i++)
{
isSet(x, i);
if (isSet = 0)
{
s = s + "0";
}
if (isSet = 1)
{
s = s + "1";
}
}
return s;
}