我有字节到二进制字符串函数,
std::string byte_to_binary(unsigned char byte)
{
int x = 128;
std::ostringstream oss;
oss << ((byte & 255) != 0);
for (int i = 0; i < 7; i++, x/=2)
oss << ((byte & x) != 0);
return oss.str();
}
Run Code Online (Sandbox Code Playgroud)
如何以相同的方式将int写入位?我不想在二进制字符串的开头加上额外的0,这就是为什么我无法弄清楚每次如何创建一个可变长度.另外,我没有使用std :: bitset.